开发者

Python: sorting an array with another array's elements as keys?

开发者 https://www.devze.com 2023-02-13 10:11 出处:网络
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 w开发者_高级运维ith more t
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 w开发者_高级运维ith more than one element is ambiguous. Use a.any() or a.all()
res = sorted(x, key=y) #TypeError: 'tuple' object is not callable


Try 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]
0

精彩评论

暂无评论...
验证码 换一张
取 消