gpt4 book ai didi

python - lexsort 负数之前的零?

转载 作者:太空宇宙 更新时间:2023-11-03 18:57:24 24 4
gpt4 key购买 nike

我有一些数据想要排序,但此方法使用 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

用在 table 上,例如

-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

但这很奇怪,对吧?

最佳答案

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

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

关于python - lexsort 负数之前的零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16982429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com