import numpy as np
data = np.array([[ 0., 1., 2., 3., 4., 5., 6.],
[ 1., 2., 1., 2., 2., 1., 1.]])
index = np.argsort(data[1], kind='mergesort') # mergesort is a bit
# slower than the default
# algorithm but is stable,
# i.e. if there's a tie
# it will preserve order
# use the index to sort both parts of data
sorted = data[:, index]
# the group labels are now in blocks, we can detect the boundaries by
# shifting by one and looking for mismatch
split_points = np.where(sorted[1, 1:] != sorted[1, :-1])[0] + 1
# could convert to int dtype here if desired
result = map(tuple, np.split(sorted[0], split_points))
# That's Python 2. In Python 3 you'd have to explicitly convert to list:
# result = list(result)
print(result)
标准的(抱歉,不是创造性的,但相当快)裸体方式是一种间接的方式:
印刷品:
^{pr2}$相关问题 更多 >
编程相关推荐