小编Gio*_*dis的帖子

在Python中排序数组而不修改特定的元素位置

我在Python中有一个numpy数组,它是n-by-n(在这个例子中是3-by-3)并且在所有对角线位置都包含零值.例如

array([[ 0.   , -0.65  ,  1.3 ,   0.56],
       [ 0.45 ,  0.    ,  0.54,   43   ],
       [ 0.5  ,  0.12  ,  0.  ,   7    ]
       [ 0.2  ,  0.3  ,  0.4  ,   0    ]])
Run Code Online (Sandbox Code Playgroud)

是否可以在不修改对角线位置的情况下对数组进行排序,以便看起来像下面的那个?因为所有排序函数都会考虑对角线位置中存在的"零"并改变它们的相对位置.

array([[ 0.   , 1.3    ,  0.56  ,   -0.65],
       [ 43   ,  0.    ,  0.54  ,   0.45   ],
       [ 7    ,  0.5   ,  0.    ,   0.12    ]
       [ 0.4  ,  0.3  ,  0.2    ,   0    ]])
Run Code Online (Sandbox Code Playgroud)

如果不能进行上述操作,那么每行中的N个最大值及其对应的索引就足够了.

直到现在我已经尝试了排序和argsort但没有结果.

python sorting numpy

5
推荐指数
1
解决办法
213
查看次数

标签 统计

numpy ×1

python ×1

sorting ×1