Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—使用递归查找列表中给定元素的索引_Python_List_Recursion_Indexing - Fatal编程技术网

Python—使用递归查找列表中给定元素的索引

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

我想要索引(所以在本例中我需要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 0
    else:
        return 1 + ind(e, L[1:])