Python—使用递归查找列表中给定元素的索引
我想要索引(所以在本例中我需要2),但代码似乎总是给我数字。我也不能在Python中使用index函数 如果你有时间,请帮忙Python—使用递归查找列表中给定元素的索引,python,list,recursion,indexing,Python,List,Recursion,Indexing,我想要索引(所以在本例中我需要2),但代码似乎总是给我数字。我也不能在Python中使用index函数 如果你有时间,请帮忙 def ind(e, L): if e not in L: return 0 else: return 1 + ind(e, L[:1]) assert ind(42, [55, 77, 42, 12, 42, 100]) def ind(e, L): if e == L[0]: return
def ind(e, L):
if e not in L:
return 0
else:
return 1 + ind(e, L[:1])
assert ind(42, [55, 77, 42, 12, 42, 100])
def ind(e, L):
if e == L[0]:
return 0
else:
return 1 + ind(e, L[1:])