设置两个数组相等 [英] Setting two arrays equal
本文介绍了设置两个数组相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
array1=[0,1,2]
array2=array1
array2[0]=234234
print array1
输出:
[234234, 1, 2]
为什么python会更改'array1'?它不应该只是更改array2吗? 更改array2时如何防止python更改array1?
Why does python change 'array1'? Shouldn't it just change array2? How can I prevent python from changing array1 when I change array2?
推荐答案
array1
和array2
是同一对象.这就是为什么改变任何一个都会改变另一个.如果您想复制对象,这是一种方法:
array1
and array2
are the same object. That's why changing either changes the other. If you want to copy the object, here's one way to do it:
array2 = array1[:]
请参见在此处进行更多的了解.
这篇关于设置两个数组相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文