給定這個 numpy 陣列:
[[0 4]
[5 0]]
我希望能夠以最小總和的順序將它們放入串列中,但我希望它能夠正常作業,不僅適用于這兩個資料,也適用于 2 個以上的資料。即它也應該適用于這個:
[[0 4]
[5 0]
[1 2]]
所以這會給:
[[1 2] [0 4] [5 0]]
這就是我得到的: order_smallest = [] def minimum_sum(xy): sums = np.sum(xy, axis=1)
while sums >= 0:
smallest_sum = np.amin(sums)
#add smallest_sum to the list order_smallest
#remove the smallest_sum from the list then?
我認為一個回圈會起作用,但我就是做錯了。
uj5u.com熱心網友回復:
使用argsort總和重新索引:
a = np.array([[0, 4],
[5, 0],
[1, 2]])
out = a[np.argsort(a.sum(axis=1))]
輸出:
array([[1, 2],
[0, 4],
[5, 0]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/531175.html
上一篇:錯誤列印的排序陣列
下一篇:將字串添加到串列的最后一個元素
