简体   繁体   English

Python:找到min元素的最后一个索引?

[英]Python: Finding the last index of min element?

For example [1,2,3,4,1,2] 例如[1,2,3,4,1,2]

has min element 1, but it occurs for the last time at index 4. 具有最小元素1,但它最后一次出现在索引4处。

>>> values = [1,2,3,4,1,2]
>>> -min((x, -i) for i, x in enumerate(values))[1]
4

No modification to the original list, works for arbitrary iterables, and only requires one pass. 不修改原始列表,适用于任意迭代,只需要一次通过。

This creates an iterable of tuples with the first value being the original element from the list, and the second element being the negated index. 这将创建一个可迭代的元组,第一个值是列表中的原始元素,第二个元素是否定索引。 When finding the minimum in this iterable of tuples the values will be compared first and then the indices, so you will end up with a tuple of (min_value, lowest_negative_index). 当在这个可迭代的元组中找到最小值时,将首先比较值,然后比较索引,因此最终会得到(min_value,lowest_negative_index)元组。 By taking the second element from this tuple and negating it again, you get the highest index of the minimum value. 通过从该元组中取出第二个元素并再次取消它,您将获得最小值的最高索引。

Here is an alternative version that is very similar, but uses a key function for min() : 这是一个非常相似的替代版本,但使用min()的键函数:

>>> min(range(len(values)), key=lambda i: (values[i], -i))
4

Note that this version will only work for sequences (lists, tuples, strings etc.). 请注意,此版本仅适用于序列(列表,元组,字符串等)。

len(list_) - list_[::-1].index(min(list_)) - 1

获取列表的长度,从反向列表中的列表的min的索引中减去,然后减去1。

a = [1,2,3,4,1,2]

a.reverse()
print len(a) - a.index(min(a)) - 1

Update after comment: 评论后更新:

The side effect can be removed by reversing again (but of course that is quite inefficient). 可以通过再次反转来消除副作用(但当然这是非常低效的)。

a.reverse()

Not so efficient (I'm a beginner in Python), but works fine as well. 效率不高(我是Python的初学者),但也可以正常工作。 The idx will just hold the last index of minimum element. idx将只保存最小元素的最后一个索引。 I think that M.Keijzers method is the best. 我认为M.Keijzers方法是最好的。

array = [1,2,3,4,1,2]
min_val = min(array) 
for i in range(len(array)):
    if array[i] == min_val:
        idx = i

print idx
>>> from operator import itemgetter
>>> from itertools import izip,count
>>> min(izip(count(len(L)-1,-1), reversed(L)), key=itemgetter(1))[0]
4

Explanation 说明

reversed returns iterator which goes through original list without creating temporary list: reversed返回迭代器,它通过原始列表而不创建临时列表:

>>> reversed(L)
<listreverseiterator object at 0x00E823B0>

izip and count are lazy, and there seems to be no byte-code execution, so I expect this solution to be pretty fast, and one-liner at that. izipcount是懒惰的,似乎没有字节码执行,所以我希望这个解决方案非常快,并且只需要一个代码。


Time comparisons 时间比较

http://ideone.com/ZQuge3 http://ideone.com/ZQuge3

Solutions using index turned out to be fastest despite they have to make 2 passes over list. 使用index解决方案最快,尽管他们必须在列表上进行2次传递。 Other solutions create auxiliary tuples inside generators on each iteration and I think it is the reason why these solutions are slower. 其他解决方案在每次迭代时在生成器内创建辅助元组,我认为这就是为什么这些解决方案速度较慢的原因。 Even akson128's solution which invokes byte-code execution is still faster (because it doesn't have to create tuples). 甚至akson128的调用字节码执行的解决方案仍然更快(因为它不必创建元组)。

len(myList)-1 - myList[::-1].index(min(list))

This uses the slice notation list[::-1] to return a shallow copy of a reversed list, so that it doesn't change your original list, then searches for the minimum value in that list 这使用切片表示法list[::-1]返回反转列表的浅表副本,这样它就不会更改原始列表,然后搜索该列表中的最小值

>>>myList = [1,2,3,4,1,2]
>>>len(myList)-1 - myList[::-1].index(min(list))
4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM