无法理解此代码 [英] having trouble understanding this code

查看:54
本文介绍了无法理解此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习递归,我有一个作业来编写一个程序,告诉列表的嵌套深度.好吧,我四处浏览并找到了可以执行此操作的工作代码,但我仍然无法理解它是如何工作的.代码如下:

def depth(L) :嵌套 = []对于 L 中的 c:如果 type(c) == type(nesting) :嵌套.追加(深度(c))如果 len(嵌套) >0:返回 1 + 最大值(嵌套)返回 1

很自然地,我开始对调用递归的附加行感到困惑.有没有人有一种简单的方法来解释这里发生的事情?我不确定实际附加了什么,并且在我的脑海中用测试用例进行检查并没有帮助.谢谢!

对不起,如果格式不好,我是用手机输入的

解决方案

让我用简单的方法告诉你,把代码改成这样:(### 是我添加到您的代码中的新行,以便您可以查看那里发生的情况)

def depth(L) :嵌套 = []对于 L 中的 c:如果 type(c) == type(nesting) :打印追加前嵌套",嵌套###嵌套.追加(深度(c))打印追加后嵌套",嵌套###如果 len(嵌套) >0:返回 1 + 最大值(嵌套)返回 1

现在让我们制作一个深度为三的列表:

l=[[1,2,3],[1,2,[4]],'asdfg']

你可以看到我们的列表有 3 个元素.其中一个是一个列表,另一个是一个本身有另一个列表的列表,最后一个是一个字符串.你可以清楚地看到这个列表的深度是 3(即有 2 个列表嵌套在主列表的第二个元素中)

让我们运行这段代码:

<预><代码>>>>深度(l)在追加之前嵌套 []追加后嵌套 [1]追加前嵌套 [1]在追加之前嵌套 []追加后嵌套 [1]追加后嵌套 [1, 2]3

小菜一碟!此函数将 1 附加到嵌套.然后,如果元素还有另一个列表,它会在嵌套中附加 1 + 最大数量,这是函数本身被调用的次数.如果元素是一个字符串,它会跳过它.

最后,它返回嵌套中的最大数量,即递归发生的最大次数,也就是主列表中的列表内列表的次数,也就是深度.在我们的例子中,正如我们预期的那样,对于第二个元素 + 1=3 递归发生了两次.

如果您仍然无法获取它,请尝试向该函数添加更多print 语句或其他变量,并仔细观察它们,最终您会得到它.

I just started learning recursion and I have an assignment to write a program that tells the nesting depth of a list. Well, I browsed around and found working code to do this, but I'm still having trouble understanding how it works. Here's the code:

def depth(L) :
    nesting = [] 
    for c in L:
        if type(c)  == type(nesting) :
            nesting.append(depth(c)) 
    if len(nesting)  > 0:
        return 1 + max(nesting)
    return 1

So naturally, I start to get confused at the line with the append that calls recursion. Does anyone have a simple way of explaining what's going on here? I'm not sure what is actually being appended, and going through it with test cases in my head isn't helping. Thanks!

edit: sorry if the formatting is poor, I typed this from my phone

解决方案

Let me show it to you the easy way, change the code like this: (### are the new lines I added to your code so you can watch what is happening there)

def depth(L) :
    nesting = []
    for c in L:
        if type(c)  == type(nesting) :
            print 'nesting before append', nesting ###      
            nesting.append(depth(c))
            print 'nesting after append', nesting ###
    if len(nesting)  > 0:
        return 1 + max(nesting)
    return 1

Now lets make a list with the depth of three:

l=[[1,2,3],[1,2,[4]],'asdfg']

You can see our list has 3 element. one of them is a list, the other is a list which has another list in itself and the last one is a string. You can clearly see the depth of this list is 3 (i.e there are 2 lists nested together in the second element of the main list)

Lets run this code:

>>> depth(l)
nesting before append []
nesting after append [1]
nesting before append [1]
nesting before append []
nesting after append [1]
nesting after append [1, 2]
3

Piece of cake! this function appends 1 to the nesting. then if the element has also another list it appends 1 + maximum number in nesting which is the number of time function has been called itself. and if the element is a string, it skips it.

At the end, it returns the maximum number in the nesting which is the maximum number of times recursion happened, which is the number of time there is a list inside list in the main list, aka depth. In our case recursion happened twice for the second element + 1=3 as we expected.

If you still have problem getting it, try to add more print statements or other variables to the function and watch them carefully and eventually you'll get it.

这篇关于无法理解此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆