我需要根据第一个子数组中的值尽可能快地排序多维数组(该行应用了数百万次)。快速排序多维数组
下面是我的原始线,我的尝试在改善其性能不起作用。据我所见,我的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))
马上,您可以尝试用'operator.itemgetter(0)'替换'lambda x:x [0]'。 – chepner
谢谢,我现在就试试。但为什么'numpy'方法不起作用?我究竟做错了什么? – Gabriel