用del()删除列表中的许多元素

时间:2023-01-30 22:45:32

I know how to delete one element of a list but if I'm trying to delete many elements I get a syntax error but don't know why.

我知道如何删除列表中的一个元素,但是如果我想删除很多元素,就会出现语法错误,但我不知道原因。

a=[[00],[01],[10],[11]]
b=[0,3]

[[del a[x]] for x in b]

so the result should looks like:

所以结果应该是:

a = [[01],[10]]

Well thank you...I understand the problem...del changes the index of array a so I would be out of bounds! :)

谢谢你……我理解这个问题……德尔改变了数组a的索引,这样我就超出界限了!:)

Now another question refer to the question... if I got a,c and I want to create b

现在另一个问题是……如果我有a c,我想要创建b

a=[[00],[01],[10],[11]]
c=[[1],[2,3,4],[5,6],[7]]

I go in that way.

我走那条路。

b = [i for i,el in enumerate(c) for item in el if len(el)<2]

and then I do this

然后我做这个

a = [x for i, x in enumerate(a) if i not in b]

is there a simple way to do that? Creating b and then "deleting" the elements of b in a ?

有没有一种简单的方法?创建b,然后“删除”a中的b元素?

4 个解决方案

#1


3  

Using list comprehension:

使用列表理解:

>>> a=[[00],[01],[10],[11]]
>>> b=[0,3]
>>> # b = set(b)
>>> a = [x for i, x in enumerate(a) if i not in b]
>>> a
[[1], [10]]

#2


1  

if you need to delete items inplace, you can do this:

如果你需要删除项目的位置,你可以这样做:

map(lambda i: a.pop(i), sorted(b, key=lambda i: -i))

or

for i in sorted(b, key=lambda x: -x):
    del a[i]

or

for i in sorted(b)[::-1]:
    del a[i]

You have to sort items in b before deletion, so you won't have out of range exception

在删除之前,必须对b中的项进行排序,这样就不会出现超出范围的异常

#3


1  

numpy has a pretty convenient way of deleting elements:

numpy有一个非常方便的删除元素的方法:

>>> import numpy as np
>>> a = np.array([[00],[01],[10],[11]])
>>> b = np.array([0,3])
>>> a
array([[ 0],
       [ 1],
       [10],
       [11]])
>>> b
array([0, 3])
>>> np.delete(a, b, axis=0)
array([[ 1],
       [10]])

#4


1  

If you remove elements from the end of the list, you will not get the Exception IndexError: list assignment index out of range, because when you delete an element, only those elements after it are affected :

如果从列表末尾删除元素,就不会出现异常IndexError:列表分配索引超出范围的情况,因为当您删除一个元素时,只有在它被影响后的元素:

>>> a=[[00],[01],[10],[11]]
>>> b=[0,3]
>>> for i in sorted(b, reverse=True):
...     del a[i]
...
>>> a
[[1], [10]]

#1


3  

Using list comprehension:

使用列表理解:

>>> a=[[00],[01],[10],[11]]
>>> b=[0,3]
>>> # b = set(b)
>>> a = [x for i, x in enumerate(a) if i not in b]
>>> a
[[1], [10]]

#2


1  

if you need to delete items inplace, you can do this:

如果你需要删除项目的位置,你可以这样做:

map(lambda i: a.pop(i), sorted(b, key=lambda i: -i))

or

for i in sorted(b, key=lambda x: -x):
    del a[i]

or

for i in sorted(b)[::-1]:
    del a[i]

You have to sort items in b before deletion, so you won't have out of range exception

在删除之前,必须对b中的项进行排序,这样就不会出现超出范围的异常

#3


1  

numpy has a pretty convenient way of deleting elements:

numpy有一个非常方便的删除元素的方法:

>>> import numpy as np
>>> a = np.array([[00],[01],[10],[11]])
>>> b = np.array([0,3])
>>> a
array([[ 0],
       [ 1],
       [10],
       [11]])
>>> b
array([0, 3])
>>> np.delete(a, b, axis=0)
array([[ 1],
       [10]])

#4


1  

If you remove elements from the end of the list, you will not get the Exception IndexError: list assignment index out of range, because when you delete an element, only those elements after it are affected :

如果从列表末尾删除元素,就不会出现异常IndexError:列表分配索引超出范围的情况,因为当您删除一个元素时,只有在它被影响后的元素:

>>> a=[[00],[01],[10],[11]]
>>> b=[0,3]
>>> for i in sorted(b, reverse=True):
...     del a[i]
...
>>> a
[[1], [10]]