令人困惑的Python列表:这是什么? [英] Confusing [...] List in Python: What is it?

查看:86
本文介绍了令人困惑的Python列表:这是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用Python编写了一个简单的二叉树,遇到了[...]

我不认为这与Ellipsis对象有关,更多的是它与无限循环有关(由于Python的浅表复制?).我完全迷失了这个无限循环的源头,以及为什么在访问时扩展时没有扩展的原因,但是我完全不知道该如何做.

使用a + b的版本

def Keys(x,y=[]):
    if len(x):y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)#Though it seems I was using y=y[:]+, this actually outputs an ugly mess
    return y

使用[a,b]

的版本

def Keys(x,y=[]):
    if len(x):y+=[x[2],Keys(x[0],y),Keys(x[1],y)]
    return y

那么[...]到底是什么?

解决方案

如果您的圆形结构中有一个指向其自身的列表,则它也会出现.像这样:

>>> a = [1,2]
>>> a.append(a)
>>> a
[1, 2, [...]]
>>> 

由于python无法打印出结构(这将是一个无限循环),因此它使用省略号来表明结构中存在递归.


我不太确定问题是怎么回事或如何解决,但我将尝试更正上面的功能.

在它们两个中,您首先进行两个递归调用,这将数据添加到列表y,然后再次将返回的数据附加到y.这意味着相同的数据将多次出现在结果中.

要么只是收集所有数据,而无需添加任何y,例如

return [x[2]]+keys(x[0])+keys(x[1])

或仅在呼叫中添加诸如

之类的内容

y += [x[2]]
keys(x[0], y) #Add left children to y...
keys(x[1], y) #Add right children to y...
return y

(当然,这两个摘要都需要处理空列表等)

@Abgan还指出,您确实不希望在初始化程序中使用y=[].

So I was writing up a simple binary tree in Python and came across [...]

I don't believe this to be related to the Ellipsis object, more it seems to have something to do with an infinity loop (due to Python's shallow copy?). The source of this infinity loop and why it doesn't get expanded while expanding when accessed is something I'm completely lost to, however

>>> a
[[[[[], [], 8, 3], [[], [], 3, 2], 6, 3], [], 1, 4], [[], [], -4, 2], 0, 0]
>>> Keys(a)#With a+b
[0, 1, 6, 8, 3, -4]
>>> Keys(a)#With [a,b]
[8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]]
>>> Keys(a)[1]#??
[8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...], 8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]]

Version using a+b

def Keys(x,y=[]):
    if len(x):y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)#Though it seems I was using y=y[:]+, this actually outputs an ugly mess
    return y

version using [a,b]

def Keys(x,y=[]):
    if len(x):y+=[x[2],Keys(x[0],y),Keys(x[1],y)]
    return y

So what exactly is [...]?

解决方案

It can also appear if you have a circular structure with a list pointing to itself. Like this:

>>> a = [1,2]
>>> a.append(a)
>>> a
[1, 2, [...]]
>>> 

Since python can't print out the structure (it would be an infinite loop) it uses the ellipsis to show that there is recursion in the structure.


I'm not quite sure if the question was what what going on or how to fix it, but I'll try to correct the functions above.

In both of them, you first make two recursive calls, which add data to the list y, and then AGAIN append the returned data to y. This means the same data will be present several times in the result.

Either just collect all the data without adding to any y, with something like

return [x[2]]+keys(x[0])+keys(x[1])

or just do the appending in the calls, with something like

y += [x[2]]
keys(x[0], y) #Add left children to y...
keys(x[1], y) #Add right children to y...
return y

(Of course, both these snippets need handling for empty lists etc)

@Abgan also noted that you really don't want y=[] in the initializer.

这篇关于令人困惑的Python列表:这是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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