为什么 a[0] 会改变? [英] Why does a[0] change?

查看:38
本文介绍了为什么 a[0] 会改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时:

a = [7,3,4]
b = a
b[0] = 10

b[0]当然设置为10,但是a[0]也设置为10代码>.为什么是这样?

b[0] is of course set to 10, but a[0] is also set to 10. Why is this?

推荐答案

b = a

这使得 ba 引用同一个列表对象.如果您希望 b 引用作为 a 副本的新列表对象,请尝试:

This makes b and a reference to the same list object. If you want b to reference to a new list object that is a copy of a, try:

b = a[:]

这篇关于为什么 a[0] 会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆