python - 如何在不实例化的情况下进行赋值

标签 python

我需要从列表中删除一个项目,然后将其再次添加到同一位置,如下所示:

x = ['a','b','c']
x2 = ['a','b','c']
x.remove(x[1])
...

x = x2 
x.remove(x[0])
...

#expected result:
x == ['b','c'] #True
x2 == ['a','b','c'] #True

#real result 

1. example == True
2. example == False 

发生这种情况是因为当我使用 x = x2 时我创建了 x2 的实例上x ,但我只想分配 x2原始值 x 。我该怎么做?

最佳答案

替换这一行:

x=x2

与:

x=x2.copy()

或者:

x=x2[:]

让它发挥作用。

关于python - 如何在不实例化的情况下进行赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53992436/

相关文章:

python - 如何在python中加载Electron + EEL

python - 自动文本摘要

python pytz,DST 不包含在转换中

python - 按数字对 QListWidget 进行排序

python - 输入文件 python 中的单独组

python - 如何正确评估Python正则表达式?

python - 尝试连接到 Azure Sql 数据库时,用户 'user' 登录失败

python - 属性错误 : str has no attribute remove

python - 如何按数字顺序对python中的文件进行排序?

python - 只有一个数字特征的逻辑回归