简体   繁体   English

如果无法使用附加的变量来查找其索引,如何从列表中删除特定的对象?

[英]How to remove specific object from a list, if I can't use the variable it is attached to, to find its index?

I have this function that creates a Ball object and appends it to a list: 我有一个创建Ball对象并将其附加到列表的函数:

list_of_balls = []

def create_new_ball():
    newball = Ball()
    list_of_balls.append(newball)

I need to be able to remove specific instances of Ball from that list. 我需要能够从该列表中删除Ball的特定实例。 I have no idea how to do this. 我不知道该怎么做。 I cant use list_of_balls.index() because all the instances are attached to the private variable newball. 我不能使用list_of_balls.index(),因为所有实例都附加到私有变量newball。 When I get the list something like this is returned: 当我得到列表时,将返回如下内容:

[< main .Foo instance at 0x22149c>, < main .Foo instance at 0x22111c>, < main .Foo instance at 0x2209b4>, < main .Foo instance at 0x22129c>] [< 主要包含.foo实例在0x22149c>,<在0x22111c 主要包含.foo实例>,<在0x2209b4 主要包含.foo实例>,< 主要包含.foo在0x22129c实例>]

So I have no idea of how to aim specific instances. 因此,我不知道如何针对特定实例。

Is there a way to remove specific instances of Ball from that list? 有没有办法从该列表中删除Ball的特定实例? Each Ball instance needs to be able to remove itself from the list, but how do the instance knows who it is on the list and/or what is its index? 每个Ball实例都需要能够从列表中删除自己,但是该实例如何知道它在列表中的人和/或其索引是什么?

Edit: What I need to do is to identify them on that list (or dictionary, or other alternative). 编辑:我需要做的是在列表(或字典或其他替代方法)上标识它们。 I know how to remove items from a list, what I can't figure out is how to do it specifically. 我知道如何从列表中删除项目,但我不知道要怎么做。

I want to do exactly what aList.remove() does, but I don't have a variable to use as argument, so I need an alternative. 我想做aList.remove()的确切工作,但是我没有变量用作参数,因此我需要一个替代方法。

list.remove seems to work Ok: list.remove似乎可以正常工作:

>>> class Ball(object): pass
... 
>>> balls = [ Ball() for _ in range(10) ]
>>> balls
[<__main__.Ball object at 0xe5350>, <__main__.Ball object at 0xe52b0>, <__main__.Ball object at 0xe5250>, <__main__.Ball object at 0xe5370>, <__main__.Ball object at 0xe5390>, <__main__.Ball object at 0xe53b0>, <__main__.Ball object at 0xe53d0>, <__main__.Ball object at 0xe5410>, <__main__.Ball object at 0xe5430>, <__main__.Ball object at 0xe5450>]
>>> balls.remove(balls[4])
>>> balls
[<__main__.Ball object at 0xe5350>, <__main__.Ball object at 0xe52b0>, <__main__.Ball object at 0xe5250>, <__main__.Ball object at 0xe5370>, <__main__.Ball object at 0xe53b0>, <__main__.Ball object at 0xe53d0>, <__main__.Ball object at 0xe5410>, <__main__.Ball object at 0xe5430>, <__main__.Ball object at 0xe5450>]
>>> len(balls)
9

The real question is how do you know which Ball you want to remove? 真正的问题是您如何知道要删除哪个Ball

Based on the comments, it looks like you want all of the balls which have bounced N times to be removed: 根据评论,您似乎希望删除所有反弹N次的球:

to_remove = [x for x in balls if x.times_bounced > N]
for x in to_remove:
    balls.remove(x)

Alternatively, if you don't need to do this in place, you can create a new list with balls which haven't bounced N times: 另外,如果您不需要就地执行此操作,则可以创建一个新列表,其中包含未反弹N次的球:

balls = [ x for x in balls if x.times_bounced <= N ]

Finally, you can use this idiom and slice assignment to do the whole thing in place if you need to: 最后,如果需要执行以下操作,可以使用此惯用法和切片分配来完成整个操作:

balls[:] = [ x for x in balls if x.times_bounced <= N ]

So I have no idea of how to aim specific instances. 因此,我不知道如何针对特定实例。

You need to have some idea of how to identify the instances; 需要对如何标识实例有所了解; and in that case, you can simply carry over that into an actual working way. 在这种情况下,您可以简单地将其延续到实际的工作方式中。 For example give them a name, or an id. 例如,给他们起一个名字或一个ID。 You could then use a dictionary or something else. 然后,您可以使用字典或其他工具。 By default the instances are only identifiable by their exact identity. 默认情况下,只能通过实例的确切身份来识别实例。

Each Ball instance needs to be able to remove itself from the list 每个Ball实例都需要能够从列表中删除自己

>>> class Ball ():
        pass
>>> list_of_balls = []
>>> def create_new_ball():
        list_of_balls.append(Ball())
>>> def remove_yourself(ball):
        list_of_balls.remove(ball)
>>> for i in range(10): create_new_ball()
>>> list_of_balls
[<__main__.Ball object at 0x0000000002EB5E48>, <__main__.Ball object at 0x0000000002EBD908>, <__main__.Ball object at 0x0000000002EBDEB8>, <__main__.Ball object at 0x0000000002EBDE80>, <__main__.Ball object at 0x0000000002EBD978>, <__main__.Ball object at 0x0000000002ED41D0>, <__main__.Ball object at 0x0000000002ED4208>, <__main__.Ball object at 0x0000000002ED4240>, <__main__.Ball object at 0x0000000002ED4278>, <__main__.Ball object at 0x0000000002ED42B0>]
>>> one_ball = list_of_balls[4] # random ball, chosen by fair dice roll
>>> one_ball in list_of_balls
True
>>> remove_yourself(one_ball)
>>> one_ball in list_of_balls
False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM