Is there an efficient way to find the last matching item in a list? When working with strings, you can find the last item with rindex:
>>> a="GEORGE"
>>> a.rindex("G")
4
...But this method doesn't exist for lists:
>>> a=[ "hello", "hello", "Hi." ]
>>> a.rindex("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'rindex'
Is there a way to get this without having to construct a big loop? I'd prefer not to use the reverse method if it can be avoided, as the order is important and I'd also have to do a bit of extra math to find out where the object /would/ have been. This seems wasteful.
Edit:
To clarify, I need the index number of this item.
所有评论(0)