列表中的[...](省略号)在Python中意味着什么?

Mar*_*oma 13 python

我刚刚获得了一些python脚本的奇怪输出:

[[(7, 6), (6, 4), (7, 2)], [...], [...], [...], [(7, 6), (8, 4), (7, 2)], [...], [...], [...], [...], [...], [...], [...]]
Run Code Online (Sandbox Code Playgroud)

输出应该是元组列表的列表.但我不知道为什么会出现.

这是什么意思?

我不认为它是一个空列表,因为空列表是[].这些可能是重复的吗?

Kar*_*tel 26

这是一个递归参考.你的清单包含自己,或者至少有某种循环.

例:

x = []
x.insert(0, x)
# now the repr(x) is '[[...]]'.
Run Code Online (Sandbox Code Playgroud)

内置repr列表检测到这种情况,并且不会尝试递归到子列表上(正常情况下),因为这会导致无限递归.

请注意,...并不一定告诉您引用了哪个列表:

y, z = [], []
x = [y, z]
y.insert(0, z)
z.insert(0, y)
# looks the same as it would if y contained y and z contained z.
Run Code Online (Sandbox Code Playgroud)

所以repr不是列表的完整序列化格式.

至于为什么你得到它们:我们不是通灵的,除非我们看到代码,否则无法修复你的代码问题.