问题
Possible Duplicate:
Finding first and last index of some value in a list in Python
Hi I was wondering if someone could help me with Python. I am trying to create a code that returns the last index of the last occurrence of an item in a list in a recursive way. So in a list [1,2,3,4,5,2] the last it should return 4. It only takes in 2 variables which are the list and the item that it is searching for. If it does not find any matching variable then it returns -1.
So far I have this:
def lstIndex(lst, item):
if len(lst) == 0:
return -1
place = lst[0]
if place == item:
print(place)
#just return the index
return lst.index(place)
else:
return lstIndex(lst[1:],item)
回答1:
I'm not quite 100% sure I know what you want. Your statement that "... in a list of [1,2,3,4,5,2] the last it should return 4 ..." has me a bit confused; I think you want to return the index of the last appearance of your specified item. So, for 4 to be the result in the list specified, item must be 5.
As noted elsewhere, a recursive function would not be the most efficient or Pythonic solution here. I'd prefer a solution like the first one in nneonneo's answer.
However, if it must be recursive, I believe the code below gets what you want. Instead of stepping through the list from the front (by using [1:]), you need to step backwards by using [:-1] as the index range when passing the list in the recursive call:
def lstIndex(lst, item):
if len(lst) == 0:
return -1
elif lst[-1] == item:
return len(lst) - 1
else:
return lstIndex(lst[0:-1], item)
I tested with the following:
the_list = [1,2,3,4,5,2]
print lstIndex(the_list, 2)
print lstIndex(the_list, 1)
print lstIndex(the_list, 3)
print lstIndex(the_list, 4)
print lstIndex(the_list, 5)
print lstIndex(the_list, 6)
print lstIndex(the_list, 0)
With the following output:
5
0
2
3
4
-1
-1
回答2:
If recursion isn't necessary, you can use this:
def find_last(lst,item):
try:
return len(lst) - next(i for i,elem in enumerate(reversed(lst),1) if elem == item)
except StopIteration:
return -1
a = [1,2,3,4,5,4,3]
idx = find_last(a,4)
print a[idx]
print find_last(a,6)
回答3:
Short iterative solution:
try:
return (len(lst)-1) - lst[::-1].index(item)
except ValueError:
return -1
But, since you are explicitly looking for a recursive solution, I'll show you how it can be done recursively. However, it will not be efficient; if you want a nice, efficient, Pythonic solution you should use an iterative solution like the others have shown (or the one above).
There's actually a few ways you can do this. You can use a helper function, which takes an extra argument specifying the last index at which the value was found:
def list_rfind(lst, item):
def list_rfind_helper(i, item, last=-1):
if i >= len(lst): return last
if lst[i] == item: last = i
return list_rfind_helper(i+1, item, last)
return list_rfind_helper(0, item)
You can do it without a helper function:
def list_rfind(lst, item):
if not lst:
return -1
res = list_rfind(lst[1:], item)
if res >= 0:
return res+1
elif lst[0] == item:
return 0
else:
return -1
回答4:
lst = [1, 2, 3, 4, 3, 4]
findLast(lst, 4)
def findLast(lst, item):
for i, val in enumerate(reversed(lst)):
if val == item:
return len(lst) - (i + 1) # Return index of matched item
return -1
回答5:
Just for completeness:
def list_rfind(lst, item):
return (len(lst)-1) - sum(1 for _ in iter(reversed(lst).next, item))
来源:https://stackoverflow.com/questions/12985096/how-to-return-that-last-index-of-a-list-python