I know there is the find and rfind mathod for strings. For lists I only know the index method. is the a 'rindex' method akin to the rfind method for strings?
List Methods quection from a newbie
Collapse
X
-
You can create a function to return the last index number.
[code=Python]def rindex(s, item):
"""
Return the index of the last occurrence of 'item' in string/list 's'.
Return False if 'item' is not in 's'.
"""
i=0
while True:
try:
i = s.index(item, i)
i += 1
except:
return i-1
return False[/code]Test:[code=Python]>>> rindex([1,2,3,4,5,6,5,4 ,3,2,1,2,3,4,3, 2,1], 3)
14
>>> [/code]Comment
-
hhmmmm...That function won't work. I'll see what I can do. Thanks anywayOriginally posted by bvdetYou can create a function to return the last index number.
[code=Python]def rindex(s, item):
"""
Return the index of the last occurrence of 'item' in string/list 's'.
Return False if 'item' is not in 's'.
"""
i=0
while True:
try:
i = s.index(item, i)
i += 1
except:
return i-1
return False[/code]Test:[code=Python]>>> rindex([1,2,3,4,5,6,5,4 ,3,2,1,2,3,4,3, 2,1], 3)
14
>>> [/code]Comment
Comment