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

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

问题描述

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

我不认为这与 Ellipsis 对象有关,更多的是它似乎与无限循环有关(由于 Python 的浅拷贝?).这个无限循环的来源以及为什么它在访问时扩展时没有扩展是我完全迷失的东西,但是<代码>

<预><代码>>>>一个[[[[], [], 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, [...], [...]]>>>键(a)[1]#??[8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...], 8, [...], [...],3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]]

使用 a+b 的版本

def Keys(x,y=[]):if len(x):y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)#虽然看起来我用的是y=y[:]+,这个实际上输出了一个丑陋的烂摊子返回 y

使用 [a,b] 的版本

def Keys(x,y=[]):如果 len(x):y+=[x[2],Keys(x[0],y),Keys(x[1],y)]返回 y

那么 [...] 究竟是什么?

解决方案

如果您有一个带有指向自身的列表的循环结构,它也会出现.像这样:

<预><代码>>>>a = [1,2]>>>a.append(a)>>>一个[1, 2, [...]]>>>

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

<小时>

我不太确定问题是发生了什么或如何修复它,但我会尝试更正上述功能.

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

或者只是收集所有数据而不添加任何y,比如

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

或者只是在调用中进行附加,例如

y += [x[2]]keys(x[0], y) #将左孩子添加到y...keys(x[1], y) #给y添加右孩子...返回 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天全站免登陆