2

如何找出哪些索引属于x数组的最低(比如 5)个数?

[10.18398473, 9.95722384, 9.41220631, 9.42846614, 9.7300549 , 9.69949144, 9.86997862, 10.28299122, 9.97274071, 10.08966867, 9.7]

另外,如何直接找到排序(从低到高)的最低x数字?

4

4 回答 4

4

现有的答案很好,但如果您使用的是以下解决方案numpy

mylist = np.array([10.18398473, 9.95722384, 9.41220631, 9.42846614, 9.7300549 , 9.69949144, 9.86997862, 10.28299122, 9.97274071, 10.08966867, 9.7])
x = 5
lowestx = np.argsort(mylist)[:x]
#array([ 2,  3,  5, 10,  4])
于 2013-03-18T22:35:54.633 回答
3

你可以这样做:

>>> l = [5, 1, 2, 4, 6]
>>> sorted(range(len(l)), key=lambda i: l[i])
[1, 2, 3, 0, 4]
于 2013-03-18T22:27:09.827 回答
2
mylist = [10.18398473, 9.95722384, 9.41220631, 9.42846614, 9.7300549 , 9.69949144, 9.86997862, 10.28299122, 9.97274071, 10.08966867, 9.7]

# lowest 5
lowest = sorted(mylist)[:5]

# indices of lowest 5
lowest_ind = [i for i, v in enumerate(mylist) if v in lowest]

# 5 indices of lowest 5
import operator
lowest_5ind = [i for i, v in sorted(enumerate(mylist), key=operator.itemgetter(1))[:5]]
于 2013-03-18T22:29:20.217 回答
0
[a.index(b) for b in sorted(a)[:5]]
sorted(a)[.x]
于 2013-03-18T22:35:39.363 回答