Python:用另一个数组元素作为键对数组进行排序?

时间:2011-02-26 18:13:08

标签: python sorting

import numpy as np
x = np.array(range(10 * 30)).reshape(100, 3)
y = np.array(range(1010, 10, -10))
res = sorted(x, key = lambda y:y) #ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
res = sorted(x, key=y) #TypeError: 'tuple' object is not callable

1 个答案:

答案 0 :(得分:2)

尝试argsort:

import numpy as np
x = np.array(range(10 * 30)).reshape(100, 3)
y = np.array(range(1010, 10, -10))
args = y.argsort(axis = 0)
print x[args]