python:对我的复制变量的更改会影响原始变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19951816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:03:16  来源:igfitidea点击:

python: changes to my copy variable affect the original variable

python

提问by natsuki_2002

I've got a list that I create a copy of in order to do some manipulations while still keeping the original list. However, when I set copy_listequal to org_list, they become the same thing, and if I change copy_list, org_listchanges too. For example:

我有一个列表,我创建了一个副本,以便在保留原始列表的同时进行一些操作。但是,当我设置为copy_listequal 时org_list,它们变成了相同的东西,如果我改变了copy_listorg_list也会改变。例如:

org_list = ['y', 'c', 'gdp', 'cap']

copy_list = org_list

copy_list.append('hum')

print(copy_list)
print(org_list)

returns

返回

['y', 'c', 'gdp', 'cap', 'hum']
['y', 'c', 'gdp', 'cap', 'hum']

I don't know too much about what is actually going on but it looks like org_listis actually passing itself to copy_listso that they are actually the same thing.

我对实际发生的事情不太了解,但看起来org_list实际上是在传递给自己,copy_list因此它们实际上是同一件事。

Is there a way to make an independent copy of org_list without doing something clumsy like:

有没有办法制作 org_list 的独立副本而不会做一些笨拙的事情,例如:

copy_list = []
for i in org_list:
    copy_list.append(i)

I say this because I have the same problem with other types of variables, for example a pandas dataframe.

我这样说是因为我对其他类型的变量也有同样的问题,例如熊猫数据框。

采纳答案by yuvi

That is because in python setting a variable actually sets a reference to the variable. Almost every person learning python encounters this at some point. The solution is simply to copy the list:

那是因为在 python 设置变量中实际上设置了对变量的引用。几乎每个学习 Python 的人都会在某个时候遇到这个问题。解决方案是简单地复制列表:

copy_list = org_list[:] 

回答by John La Rooy

This is just copying the reference

这只是复制参考

copy_list = org_list

you should use

你应该使用

copy_list = org_list[:]    # make a slice that is the whole list

or

或者

copy_list = list(org_list)

回答by Andrew Jaffe

Variable names in python are references to the original. To actually make a copy, you need to be explicit:

python中的变量名是对原始变量的引用。要实际制作副本,您需要明确

import copy

copy_list = copy.copy(org_list)

回答by Roberto

When you write

当你写

org_list = ['y', 'c', 'gdp', 'cap']

you create the list object, and give it the name "org_list".

您创建列表对象,并将其命名为“org_list”。

Then when you do

然后当你做

copy_list = org_list

you just mean, "the name copy_list refers to the same object as org_list does".

您的意思是,“名称 copy_list 指代与 org_list 相同的对象”。

If your list only contains immutable types, then you can create a copy by

如果您的列表仅包含不可变类型,那么您可以通过以下方式创建副本

copy_list = list(org_list)

But note that this is only valid if the list objects are immutable, because it creates a SHALLOW copy, i.e. the list is copied, but every element on the list is not duplicated.

但请注意,这仅在列表对象是不可变的情况下才有效,因为它创建了一个浅拷贝,即列表被复制,但列表中的每个元素都不会重复。

If you have i.e. a list of lists and want EVERYTHING to be duplicated, you need to perform a DEEP copy:

如果您有一个列表列表并希望复制所有内容,则需要执行深度复制:

import copy
org_list = ['y', 'c', ['gdp', 'rtd'], 'cap']
copy_list = copy.deepcopy(org_list)