简体   繁体   English

python递归列表问题

[英]python recursive list questions

1.I came across this code: Python Recursion and list 1.我遇到了这段代码: Python递归和列表

def search(lst, key):
    if not lst:         # base case: list is empty
        return False
    elif lst[0] == key: # base case: current element is searched element
        return True
    else:               # recursive case: advance to next element in list
        return search(lst[1:], key)

Can the recursive search start from the first element as search(lst[0:],key) ? 递归搜索可以从第一个元素开始search(lst[0:],key)吗? Why is the first element treated separately? 为什么第一个元素是单独处理的?

2.Why is this a recursion? 2.为什么这是递归?

selfref_list = [1, 2, 3]
selfref_list.append(selfref_list) 

About the first question: 关于第一个问题:

If you start the recursion as search(lst[0:], key) then you will enter an infinite recursion. 如果您以search(lst[0:], key)开始递归search(lst[0:], key)那么您将进入无限递归。 Notice that for the recursion to stop you need to have each time a smaller list than before and that is the case with search(lst[1:], key) . 请注意,对于要停止的递归,您需要每次都有一个比之前更小的列表,这就是search(lst[1:], key)

Also, as to why the first element is treated separately, notice you need to perform the comparison against key one element at a time, and that element is lst[0] . 另外,关于为什么单独处理第一个元素,请注意您需要一次对key一个元素执行比较,并且该元素是lst[0]

So in this recursion, you divide the problem in two: (1) you check if some element of the list is equal to your key (this element is always the first element of the current list), and (2) a recursive call in the rest of the list. 所以在这个递归中,你将问题分成两部分:(1)你检查列表中的某个元素是否等于你的键(这个元素总是当前列表的第一个元素),以及(2)递归调用列表的其余部分

About the second question: 关于第二个问题:

This isn't a recursion: 这不是递归:

selfref_list = [1, 2, 3]
selfref_list.append(selfref_list)

What those lines do is to create a list where the 4th element is the same list: 这些行的作用是创建一个列表,其中第4个元素是相同的列表:

>>> selfref_list == selfref_list[3] 
True

But that is not related to recursion in functions. 但这与函数中的递归无关。

For the first question, in the first line, def search(lst, key) . 对于第一个问题,在第一行中, def search(lst, key) that is, every time the function is called, it will use the entire lst which is the same as lst[0:] . 也就是说,每次调用函数时,它都将使用与lst[0:]相同的整个lst Now, the reason that you have 现在,你有这个原因

    else: # recursive case: advance to next element in list
        return search(lst[1:], key)

is to define what happens after the current first element has been traversed. 是定义在遍历当前第一个元素之后发生的事情。 Basically, it says, "after the first element has been checked, continue with checking the rest of the elements starting with the next element." 基本上,它说,“在检查了第一个元素之后,继续检查从下一个元素开始的其余元素。”

Why is the first element treated separately: Because each instance of your recursive method does part of your job, the function must check an element from the array. 为什么第一个元素是单独处理的:因为递归方法的每个实例都是你工作的一部分,所以函数必须检查数组中的元素。 The First element or lst[0] will be the only element left when you call the function for a list with one element so why not just check it every time the list isn't empty? 第一个元素或lst[0]将是您为具有一个元素的列表调用函数时剩下的唯一元素,那么为什么不在每次列表不为空时检查它?

Why is this a recursion? 为什么这是一个递归?

selfref_list = [1, 2, 3] selfref_list.append(selfref_list)

In the most literal sense it is not recursion. 从字面意义上讲,它不是递归。 Rather the data type is recursive, meaning that an object may include itself in an instance of the same object type. 相反,数据类型是递归的,这意味着对象可以将自身包含在相同对象类型的实例中。 Or in more formal mathematical terms a node type in a grammar tree is permitted to have an outgoing edge to another node of similar type... 或者在更正式的数学术语中,允许语法树中的节点类型具有到相似类型的另一节点的传出边缘......

recursive grammar 递归语法

recursive data Type 递归数据类型

请注意,顶部列表包含类似的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM