python中的列表和id

问题描述

列表中的“[...]”是什么?为什么第二个输出没有双列表?我知道 a 和 b 共享相同的 ID 但是,为什么会发生这种情况?

a = [1,2,3]
b =a
b.append(4)
print(a)

output :[1,3,4]


a.append(b)
print(b)

output :[1,4,[...]]#what happend here ?

解决方法

好吧,我不知道这是否是最佳答案,请纠正我 如果我错了。

试试这个:

print(b[4][4])  # you can add as many [4] you want and i used [4] because it isthe position of [...] .

输出将相同 **Output:[1,2,3,4,[...]] **

所以我尝试获取所有 'b' 以及 'b[4]'、'b[4][4]' 的地址......

使用:

print(id(b))
print(id(b[4]))
print(id(b[4][4]))

## All these will return the same address.

因此,列表中的列表是相同的列表(a 或 b)并且它会继续,这就是为什么你会得到那些 '...'