gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:39 24 4
gpt4 key购买 nike

我正在尝试分配 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

最佳答案

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

关于python - 执行 list.pop() 时出现意外结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48678044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com