把零排在负片之前?

2024-04-20 01:21:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些数据,我想排序,但这个方法使用numpy.lexsort排序()

data = np.zeros(shape=(n,6))
# some routine that partially populates the table
index = np.lexsort((data[:,0],data[:,1]))
data = data[index] # sort

用于桌子上,如

-500    0.5 0.0 0.0 0.0 0.0
-400    0.6 0.0 0.0 0.0 0.0
0.0     0.0 0.0 0.0 0.0 0.0
0.0     0.0 0.0 0.0 0.0 0.0

返回数据,例如:

0.0     0.0 0.0 0.0 0.0 0.0
0.0     0.0 0.0 0.0 0.0 0.0
-500    0.5 0.0 0.0 0.0 0.0
-400    0.6 0.0 0.0 0.0 0.0

但这很奇怪,对吧?你知道吗


Tags: 数据方法numpydataindexthat排序np
1条回答
网友
1楼 · 发布于 2024-04-20 01:21:01

正如塞伯格所说,你所看到的并不是错误的。Lexsort按提供的最后一个键排序,然后按倒数第二个键排序,依此类推,这看起来可能有点落后。从docstring:

The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. The keys argument must be a sequence of objects that can be converted to arrays of the same shape. If a 2D array is provided for the keys argument, it's rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc.

考虑到这一点,您看到的是正确的排序,但您可能想做的是:

data = np.zeros(shape=(n,6))
# some routine that partially populates the table
index = np.lexsort((data[:,1],data[:,0]))
data = data[index] # sort

相关问题 更多 >