This question already has an answer here:
这个问题在这里已有答案:
- Circular Reference with python lists 3 answers
- 循环引用python列出3个答案
I tried to print a list which supposes to be(I think) a empty list, However I got a [[...]]
instead. I tried to google it, but no luck. Futhermore, When I use slice to see what the hell is [...
] in the strange list, I got the same [[...]]
, I guess there would be some kind of recursive, but what leads to it ?Anyone knows why?
我试图打印一个列表,假设是(我认为)一个空列表,但我得到了[[...]]。我试着谷歌,但没有运气。此外,当我使用切片查看奇怪列表中的内容时,我得到了相同的[[...]],我想会有某种递归,但是什么导致了它?谁知道为什么?
3 个解决方案
#1
6
You are correct. It's a list that contains itself recursively. Here's a simple way to create such a list:
你是对的。它是一个递归包含自己的列表。这是创建这样一个列表的简单方法:
a = []
a.append(a)
print(a)
output
产量
[[...]]
#2
4
That means a list contains itself:
这意味着列表包含自己:
a = []
a.append(a)
print(a)
#3
3
>>> a=[]
>>> a.append(a)
>>> a
[[...]]
This is one way to get that -- a list with one element, namely itself.
这是获得它的一种方法 - 一个包含一个元素的列表,即它本身。
Such a structure can't be printed (it recurses forever) so Python prints ellipsis to mean "and so on".
这样的结构不能打印(永远递归),因此Python打印省略号表示“依此类推”。
#1
6
You are correct. It's a list that contains itself recursively. Here's a simple way to create such a list:
你是对的。它是一个递归包含自己的列表。这是创建这样一个列表的简单方法:
a = []
a.append(a)
print(a)
output
产量
[[...]]
#2
4
That means a list contains itself:
这意味着列表包含自己:
a = []
a.append(a)
print(a)
#3
3
>>> a=[]
>>> a.append(a)
>>> a
[[...]]
This is one way to get that -- a list with one element, namely itself.
这是获得它的一种方法 - 一个包含一个元素的列表,即它本身。
Such a structure can't be printed (it recurses forever) so Python prints ellipsis to mean "and so on".
这样的结构不能打印(永远递归),因此Python打印省略号表示“依此类推”。