[英]Find index of element in a list using recursion
def index(L,v)
''' Return index of value v in L '''
pass
I need help with implementing this function using recursion.我需要帮助来实现这个 function 使用递归。 Really new to recursion stuff so any advice would help
对递归的东西真的很陌生,所以任何建议都会有所帮助
Note that L
is a list.请注意,
L
是一个列表。 v
is a value. v
是一个值。
That works那个有效
def recursive_index(L, v):
return 0 if L[0] == v else 1 + recursive_index(L[1:], v)
but is pretty stupid (and will only work if the value exists)但非常愚蠢(并且只有在值存在时才有效)
You can add if v not in L: return -1
to make it work for any case, but that is even worst.您可以添加
if v not in L: return -1
以使其适用于任何情况,但这更糟糕。
Do it really has to be recursive?它真的必须是递归的吗?
I assume this is homework.我认为这是家庭作业。
So you need to understand recursion.所以你需要了解递归。 Here's an example:
下面是一个例子:
def countdown(n):
if n == 0:
print "Hello World!"
else:
print n
countdown(n-1)
You need to start with a starting point, in your case it would probably be the 0th element.您需要从一个起点开始,在您的情况下,它可能是第 0 个元素。
You need an end point, which should be the length - 1
or when you find the element.您需要一个终点,它应该是
length - 1
或您找到元素的时间。
Simple if else should do here, with a modified version of countdown as above.简单的 if else 应该在这里做,修改后的倒计时版本如上。
Yet another way:还有一种方式:
def rec(l,v, index=0):
try:
if l[index] == v:
return index
except IndexError:
return -1
return rec(l,v,index+1)
Why would someone write recursive code for that??为什么有人会为此编写递归代码?
>>> [1,2,4,8].index(4)
2
L = [1, 2, 3, 4, 5, 6, 7, 11, 13]
def index(L, v):
if len(L) == 0:
return -1000000
elif L[0] == v:
return 0
else:
return 1 + index(L[1:], v)
print index(L, 7)
print index(L, 13)
print index(L, 100)
* Remote Interpreter Reinitialized * *远程解释器重新初始化*
6 6
8 8
-999991 -999991
Assuming 0 indexing, the following code will return the index of the element if it exists, or -1 if it is not contained in the list:假设索引为 0,以下代码将返回元素的索引(如果存在),如果元素不包含在列表中,则返回 -1:
def index(L, v):
if L == []:
return -1
elif L[0] == v:
return 0
rv = index(L[1:], v)
if rv < 0:
return rv
return rv + 1
Here a tail recursive version of it:这是它的尾递归版本:
def indexof(elem, list_):
return indexof_tailrec(elem, list_, 0)
def indexof_tailrec(elem, list_, index):
if index >= len(list_):
return None
if list_[index] == elem:
return index
return indexof_tailrec(elem, list_, index + 1)
Note, however, that Python does not have tail call optimization (at least not as far as I know).但是请注意,Python 没有尾调用优化(至少据我所知没有)。
A recursive solution to find the first occurence of an element using Binary Search is this -使用二进制搜索查找元素第一次出现的递归解决方案是 -
def Binary_Search(l, val, low, high):
if low > high:
return -1
mid = (low+high)//2
if len(l) == 1:
if l[0] == val:
return 0
return -1
if l[mid] == val:
if mid == 0 or l[mid-1] != l[mid]:
return mid
else:
return Binary_Search(l, val, low, mid-1)
elif l[mid] < val:
return Binary_Search(l, val, mid+1, high)
else:
return Binary_Search(l, val, low, mid-1)
def fi(arr,x):
if len(arr)==0:
return -1
elif arr[0]==x:
return 0
k= fi(arr[1:],x)
if k== -1:
return -1
else:
return k+1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.