python finds a way to index the largest or smallest numbers in list
- 2021-01-14 06:12:22
- OfStack
This is as follows:
nums = [1,8,2,23,7,-4,18,23,24,37,2]
result = map(nums.index, heapq.nlargest(3, nums))
temp=[]
Inf = 0
for i in range(3):
temp.append(nums.index(max(nums)))
nums[nums.index(max(nums))]=Inf
result.sort()
temp.sort()
print(result)
print(temp)
As shown above, there are two ways to find result and temp. The above code outputs:
[3, 8, 9]
[3, 8, 9]
no problem
But change nums to 1:
nums = [1,8,2,23,7,-4,18,23,23,37,2]
Output:
[3, 3, 9]
[3, 7, 9]
The result method finds that the same number always returns the index that appears the first time.