列表中的省略号 [...] 是什么意思? [英] What do ellipsis [...] mean in a list?
问题描述
我在玩 python.我在 IDLE 中使用了以下代码:
I was playing around in python. I used the following code in IDLE:
p = [1, 2]
p[1:1] = [p]
print p
输出是:
[1, [...], 2]
这是什么[…]
?有趣的是,我现在可以使用它作为一个列表列表,直到无穷大,即
What is this […]
? Interestingly I could now use this as a list of list of list up to infinity i.e.
p[1][1][1]....
我可以随心所欲地写上面的内容,它仍然有效.
I could write the above as long as I wanted and it would still work.
- 它在内存中是如何表示的?
- 它有什么用?一些有用的案例示例会有所帮助.
- 任何指向官方文档的链接都会非常有用.
推荐答案
这意味着您创建了一个嵌套在其内部的无限列表,该列表无法打印.p
包含 p
包含 p
... 等等.[...]
符号是一种让您知道这一点的方式,并告知它无法表示!看看@6502 的回答,可以看到一张很好的图片,展示了正在发生的事情.
It means that you created an infinite list nested inside itself, which can not be printed. p
contains p
which contains p
... and so on. The [...]
notation is a way to let you know this, and to inform that it can't be represented! Take a look at @6502's answer to see a nice picture showing what's happening.
现在,关于编辑后的三个新项目:
Now, regarding the three new items after your edit:
- This answer seems to cover it
- Ignacio's link describes some possible uses
- This is more a topic of data structure design than programming languages, so it's unlikely that any reference is found in Python's official documentation
这篇关于列表中的省略号 [...] 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!