【发布时间】:2018-02-08 05:09:39
【问题描述】:
我需要尽可能快地根据第一个子数组中的值对多维数组进行排序(这条线被应用了数百万次)。
以下是我的原始行,以及我试图提高其性能的尝试,但这是行不通的。据我所知,我的numpy 方法只正确排序第一个子数组,其余的都没有。
我做错了什么,如何提高排序的性能?
import numpy as np
# Generate some random data.
# I receive the actual data as a list, hence the .tolist()
aa = np.random.rand(10, 2000).tolist()
# This is the original line I need to process faster.
b1 = zip(*sorted(zip(*aa), key=lambda x: x[0]))
# This is my attempt at improving the above line's performance
b2 = np.sort(np.asarray(aa).T, axis=0).T
# Check if all sub-arrays are equal
for a, b in zip(*[b1, b2]):
print(np.array_equal(a, b))
【问题讨论】:
-
马上,您可以尝试将
lambda x: x[0]替换为operator.itemgetter(0)。 -
谢谢,我现在就试试。但是为什么
numpy方法不起作用?我做错了什么?
标签: python arrays sorting numpy