.remove() 正在从两个变量(列表)(我将它们设置为彼此相等)中删除元素,而我只想从一个变量中删除它
.remove() is removing elements from both variables (lists) (which I set equal to each other) when I only want to remove it from one variable
我有一个名为 ranks
的列表和一个名为 modelRanks
的列表,它们是相等的。当我遍历我的 for 循环时,我想从 modelRanks
中删除在 ranks
中计数为 2 或更多的任何内容。我使用了 remove
函数,当我调用它以仅从 modelRanks
.
中删除时,它正在从两者中删除元素
我也试过 modelRanks = modelRanks.remove(num)
和 modelRanks -= [num]
这是我目前的代码。我正在使用 python 3.7.
def modelForPairs(ranks):
modelRanks = ranks
for num in ranks:
count = ranks.count(num)
if count > 1:
modelRanks.remove(num)
return modelRanks
如果我的排名列表是 [11, 2, 3, 11, 3]
,我希望我的 modelRanks
最终看起来像 [2]
。如果 ranks = [2, 4, 3, 11, 3]
我希望 modelRanks
最终看起来像 [2, 4, 11]
等等
初始化时使用深度复制:
替换
modelRanks = ranks
和
modelRanks = copy.deepcopy(ranks)
还有 import copy
在 运行 模块之前。
在您的代码中,只需将行 modelRanks = ranks
更改为 modelRanks = ranks.copy()
。
在Python中,设置一个等于列表的变量只是将它指向那个列表对象;默认情况下它不会创建副本。例如:
lst = [1, 2, 3]
lst2 = lst
lst2.remove(2)
print(lst2)
# [1, 3]
print(lst)
# [1, 3]
# They're the same object!
您可以通过显式调用 lst.copy()
或 lst[:]
(see the docs) 来解决这个问题。这两者都执行所谓的 浅拷贝.
lst = [1, 2, 3]
lst2 = lst.copy() # or lst2 = lst[:]
lst2.remove(2)
print(lst2)
# [1, 3]
print(lst)
# [1, 2, 3]
# Different objects!
关于浅与深拷贝的说明(因为我看到很多证据表明在这一点上存在一些混淆) .来自 docs:
The difference between shallow and deep copying is only relevant for
compound objects (objects that contain other objects, like lists or
class instances):
A shallow copy constructs a new compound object and then (to the
extent possible) inserts references into it to the objects found in
the original.
A deep copy constructs a new compound object and then, recursively,
inserts copies into it of the objects found in the original.
基本上,如果您的列表包含其他对象,并且您明确需要避免仅指向 new 列表中的 same 个对象,只有这样你才需要使用深拷贝。浅拷贝将填充 new 列表,其中包含指向 same 对象的指针。从此列表中删除项目不会以任何方式影响原始对象,只会将它们从 new 列表中删除。
深拷贝会递归地为列表中的所有内容创建全新的对象。这通常是不必要的,但最好了解您在某些情况下的区别,例如,您想要将项目复制到新列表中并且 manipulate 仅那些新列表中的项目,不是复制它们的原始项目。
如果其中 none 对您有意义,请不要担心。以后您可能 运行 会因复制列表而头疼,在这种情况下请记住查看深拷贝与浅拷贝。
在你的第一行:(modelRanks = ranks)
你正在做浅拷贝。
这意味着两个变量都指向内存中的相同位置。因此,当您更改一个变量时,更改也会通过第二个变量可见。
要深度复制列表,请使用 copy.deepcopy(...),您的第二个变量将拥有自己的内存 space。
或者简单地使用 Counter
:
from collections import Counter
c = Counter(ranks)
modelRanks = list(item for item, count in c.items() if count == 1)
所以这里发生的事情是,当您将两个列表设置为彼此相等时,您正在将第一个变量的引用复制到第二个变量。
我的意思是:
假设一个数组被分配给 a
。当我们写表达式 b = a
时,b
指向与 a
相同的数组。
所以这个图表可能看起来像:
这意味着如果 a
改变,那么 b
也会改变。因此,如果我们不想使用 a.copy()
方法更改 b
,我们必须创建列表 a
的副本。
我有一个名为 ranks
的列表和一个名为 modelRanks
的列表,它们是相等的。当我遍历我的 for 循环时,我想从 modelRanks
中删除在 ranks
中计数为 2 或更多的任何内容。我使用了 remove
函数,当我调用它以仅从 modelRanks
.
我也试过 modelRanks = modelRanks.remove(num)
和 modelRanks -= [num]
这是我目前的代码。我正在使用 python 3.7.
def modelForPairs(ranks):
modelRanks = ranks
for num in ranks:
count = ranks.count(num)
if count > 1:
modelRanks.remove(num)
return modelRanks
如果我的排名列表是 [11, 2, 3, 11, 3]
,我希望我的 modelRanks
最终看起来像 [2]
。如果 ranks = [2, 4, 3, 11, 3]
我希望 modelRanks
最终看起来像 [2, 4, 11]
等等
初始化时使用深度复制:
替换
modelRanks = ranks
和
modelRanks = copy.deepcopy(ranks)
还有 import copy
在 运行 模块之前。
在您的代码中,只需将行 modelRanks = ranks
更改为 modelRanks = ranks.copy()
。
在Python中,设置一个等于列表的变量只是将它指向那个列表对象;默认情况下它不会创建副本。例如:
lst = [1, 2, 3]
lst2 = lst
lst2.remove(2)
print(lst2)
# [1, 3]
print(lst)
# [1, 3]
# They're the same object!
您可以通过显式调用 lst.copy()
或 lst[:]
(see the docs) 来解决这个问题。这两者都执行所谓的 浅拷贝.
lst = [1, 2, 3]
lst2 = lst.copy() # or lst2 = lst[:]
lst2.remove(2)
print(lst2)
# [1, 3]
print(lst)
# [1, 2, 3]
# Different objects!
关于浅与深拷贝的说明(因为我看到很多证据表明在这一点上存在一些混淆) .来自 docs:
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
基本上,如果您的列表包含其他对象,并且您明确需要避免仅指向 new 列表中的 same 个对象,只有这样你才需要使用深拷贝。浅拷贝将填充 new 列表,其中包含指向 same 对象的指针。从此列表中删除项目不会以任何方式影响原始对象,只会将它们从 new 列表中删除。
深拷贝会递归地为列表中的所有内容创建全新的对象。这通常是不必要的,但最好了解您在某些情况下的区别,例如,您想要将项目复制到新列表中并且 manipulate 仅那些新列表中的项目,不是复制它们的原始项目。
如果其中 none 对您有意义,请不要担心。以后您可能 运行 会因复制列表而头疼,在这种情况下请记住查看深拷贝与浅拷贝。
在你的第一行:(modelRanks = ranks) 你正在做浅拷贝。 这意味着两个变量都指向内存中的相同位置。因此,当您更改一个变量时,更改也会通过第二个变量可见。
要深度复制列表,请使用 copy.deepcopy(...),您的第二个变量将拥有自己的内存 space。
或者简单地使用 Counter
:
from collections import Counter
c = Counter(ranks)
modelRanks = list(item for item, count in c.items() if count == 1)
所以这里发生的事情是,当您将两个列表设置为彼此相等时,您正在将第一个变量的引用复制到第二个变量。
我的意思是:
假设一个数组被分配给 a
。当我们写表达式 b = a
时,b
指向与 a
相同的数组。
所以这个图表可能看起来像:
这意味着如果 a
改变,那么 b
也会改变。因此,如果我们不想使用 a.copy()
方法更改 b
,我们必须创建列表 a
的副本。