Python 我能';t从列表中删除类对象
所以我有这个列表(Sheep是我创建的一个类,需要一个参数): 但是,当我尝试从列表中删除Python 我能';t从列表中删除类对象,python,list,Python,List,所以我有这个列表(Sheep是我创建的一个类,需要一个参数): 但是,当我尝试从列表中删除Sheep(2)(list.remove(Sheep(2))时,它返回ValueError:list.remove(x):x不在列表中 如何修复此问题?您可以使用索引删除对象 x = object() y = object() array = [x, y] array.pop(0) # Using the del statement del array[0] 看看这个: class Sheep:
Sheep(2)
(list.remove(Sheep(2)
)时,它返回ValueError:list.remove(x):x不在列表中
如何修复此问题?您可以使用索引删除对象
x = object()
y = object()
array = [x, y]
array.pop(0)
# Using the del statement
del array[0]
看看这个:
class Sheep:
def __init__(self,v):
self.v = v
创建实例时:
In [3]: Sheep(1)
Out[3]: <__main__.Sheep at 0x7fa5a7614550>
In [4]: Sheep(1)
Out[4]: <__main__.Sheep at 0x7fa5a49bf4a8>
这是因为创建实例,每次创建具有相同值的实例时,并不意味着它们是相同的
但你可以这样做:
In [6]: Sheep(1).v == Sheep(1).v
Out[6]: True
因此,如果要删除具有v=2
的项,可以执行以下操作:
list_of_sheeps = [Sheep(2), Sheep(3), Sheep(3), Sheep(4), Sheep(4), Sheep(4), Sheep(5), Sheep(5), Sheep(5), Sheep(5), Sheep(6), Sheep(6), Sheep(6), Sheep(7), Sheep(7), Sheep(8)]
new_list = [i for i in list_of_sheeps if i.v != 2] # v is the name of inner field. (self.v)
然后,新列表
将是您想要的结果,而不需要绵羊(2)
或使用过滤器:
new_list = list(filter(lambda x:x.v != 2, list_of_sheeps))
请注意:
不要使用list
作为变量名。list是python中的内置名称,您将覆盖它自己的功能
您可以通过检查如下值,使用列表理解删除其他实例:
>>> list = [Sheep(2), Sheep(3), Sheep(3), Sheep(4), Sheep(4), Sheep(4), Sheep(5), Sheep(5), Sheep(5), Sheep(5), Sheep(6), Sheep(6), Sheep(6), Sheep(7), Sheep(7), Sheep(8)]
>>> newl = [x for x in list if x.camo != 2] # will remove all Sheep with camo value == 2
要仅删除一个值为的实例,可以执行以下操作:
>>> for idx, sheep in enumerate(list[:]):
... if sheep.camo == 3:
... list.pop(idx)
... break
但是我建议您在Sheep
类中实现\uuuuu eq\uuuu
方法,只需这样做
>>> list.remove(Sheep(3))
而现在新的名单将是这样的
>>> newl
[Sheep(3), Sheep(3), Sheep(4), Sheep(4), Sheep(4), Sheep(5), Sheep(5), Sheep(5), Sheep(5), Sheep(6), Sheep(6), Sheep(6), Sheep(7), Sheep(7), Sheep(8)]
两只羊(2)
s是不同的对象
x = object()
y = object()
array = [x, y]
array.pop(0)
# Using the del statement
del array[0]
要通过将第一只绵羊(2)
与第二只进行比较来找到第一只绵羊(2)
,请在绵羊
上定义相等
class Sheep:
def __init__(self, camouflage_score):
self.camo = camouflage_score
def __eq__(self, other):
return other and self.camo == other.camo
要测试自定义类在python列表中的成员身份,必须提供一个相等(\uuuuuuuueq\uuuuuuu
)方法
现在,您可以使用相同的“x”创建类的实例,它们将成为列表的成员
foo = [Sheep(2), Sheep(3), Sheep(3), Sheep(4), Sheep(4), Sheep(4), Sheep(5), Sheep(5), Sheep(5), Sheep(5), Sheep(6), Sheep(6), Sheep(6), Sheep(7), Sheep(7), Sheep(8)]
print(Sheep(2) in foo) # True
而且你也应该能够移除它们
foo.remove(Sheep(2))
print(foo) # [Sheep(3), Sheep(3), Sheep(4), Sheep(4), Sheep(4), Sheep(5), Sheep(5), Sheep(5), Sheep(5), Sheep(6), Sheep(6), Sheep(6), Sheep(7), Sheep(7), Sheep(8)]
这是因为两次编写Sheep(2)
会创建两个不同的Sheep
实例Sheep
类的定义是什么?您可以粘贴它吗?这里有几个问题:(1)不要使用保留字,即list,作为变量名;(2)您对list.remove的调用将创建一个新的Sheep对象,而不是引用现有对象。我建议在将对象添加到列表之前,使用索引删除该对象或命名这些对象。class Sheep:def _uinit__;(self,camovage_score)Self.CAMO=CasouFraceGraceDo,你想从列表中删除值为2的所有<代码>羊<代码>吗?但是有可能移除特定的羊吗?例如,在列表中间的羊(你不知道索引)或者你如何找到一只羊的索引?使用这种方法,这是不可能的!有没有一种方法可以在不使用索引的情况下提到一只特定的羊?你可以只删除一只吗?例如…如果我只想删除一只羊(3)?当然。你可以在羊类中实现eq\uuuuuuuu
方法,然后只删除列表。删除(绵羊(3))
foo = [Sheep(2), Sheep(3), Sheep(3), Sheep(4), Sheep(4), Sheep(4), Sheep(5), Sheep(5), Sheep(5), Sheep(5), Sheep(6), Sheep(6), Sheep(6), Sheep(7), Sheep(7), Sheep(8)]
print(Sheep(2) in foo) # True
foo.remove(Sheep(2))
print(foo) # [Sheep(3), Sheep(3), Sheep(4), Sheep(4), Sheep(4), Sheep(5), Sheep(5), Sheep(5), Sheep(5), Sheep(6), Sheep(6), Sheep(6), Sheep(7), Sheep(7), Sheep(8)]