Remove object from a list of objects in python

后端 未结 7 1392
醉酒成梦
醉酒成梦 2020-12-09 14:20

In Python, how can I remove an object from array of objects? Like this:

x = object()
y = object()
array = [x,y]
# Remove x

I\'ve tried

7条回答
  •  情书的邮戳
    2020-12-09 15:14

    You could try this to dynamically remove an object from an array without looping through it? Where e and t are just random objects.

    >>> e = {'b':1, 'w': 2}
    >>> t = {'b':1, 'w': 3}
    >>> p = [e,t]
    >>> p
    [{'b': 1, 'w': 2}, {'b': 1, 'w': 3}]
    >>>
    >>> p.pop(p.index({'b':1, 'w': 3}))
    {'b': 1, 'w': 3}
    >>> p
    [{'b': 1, 'w': 2}]
    >>>
    

提交回复
热议问题