本文介绍了Python编程:列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gene = [0,5,1,0,7,1]

originalGene = gene



gene [0] = 2

打印originalGene



输出:[2,5,1,0,7,1]

问题:为什么'originalGene'列表的内容也会改变?



我尝试过:



我试图多次运行代码,我尝试使用变量进行操作,但它与变量一起工作,但没有列表......

gene = [0, 5, 1, 0, 7, 1]
originalGene = gene

gene[0] = 2
print originalGene

OUTPUT: [2, 5, 1, 0, 7, 1]
QUESTION: Why do the contents of the 'originalGene' list get changed as well ?

What I have tried:

I have tried to run the code multiple times and I have tried doing it with variables and it WORKS with variables but not with lists...

推荐答案


gene = [0, 5, 1, 0, 7, 1] 
originalGene = gene[:]



这篇关于Python编程:列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:27