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

标签 python python-3.x python-2.7 variable-assignment

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

相关文章:

python - 通过 beautifulsoup 获取文本而不使用 str.text.strip()

python - GUI 在循环时变得无响应

python - 使用 PyAudio 和 TK 在按键上演奏音符

python - 导入错误 : No module named scipy

python - 对于 Python 3 中的随机列表,如何让列表接受小写字母代替 Hangman 中的大写字母?

python - python模块是否被导入两次?

python - Python 2 中的类型提示

python - 获取顶级 Python 脚本的路径

python - 适用于 Python 3.4.4 的 Jinja2 版本

Magento REST API,用户授予管理员角色,但仅授予访客访问权限