执行 list.pop() 时出现意外结果?

Unexpected result while executing list.pop()?

我正在尝试分配 a = x 和 b = x.pop();尽管我得到了意想不到的任务。你能解释一下吗?

>>> x = [10, 11, 12, 13]
>>> a, b = x, x.pop(2)
>>> print a
[10, 11, 13] # Shouldn't I get a = [10, 11, 12, 13]?
>>> print b
12

当你说 a=x 时,ax 都指向相同的列表,因此修改 a 也会修改 x。如果你说 a=list(x) 那么 a 将是列表 x.

的单独副本

Since you're referring direct list, it's popping and assigning what left in list. If you have list as copy then try this:-

x = [10, 11, 12, 13]
a,b = x.copy(),x.pop(2)
print(a) # your expected output

你会得到 a = [10,11,13], b = 12.

执行 'b = x.pop()' 后弹出 x 的值并且 x 永远改变,即在执行值 'b'

后程序的其余部分