简体   繁体   English

在Python中,如何从列表列表中删除某个元素?

[英]In Python, how can I remove a certain element from a list of lists?

Say I have a list of lists: 说我有一个清单清单:

x = [[1,0,0,3],[1,1,1,1],[5,2,0,0],[4,3,0,1],[0,0,0,0]

How can I create a new list that only contains the non-zero terms of each list, such that 如何创建一个仅包含每个列表的非零项的新列表,例如

y = [[1,3],[1,1,1,1],[5,2],[4,3,1],[]]
In [23]: x = [[1,0,0,3],[1,1,1,1],[5,2,0,0],[4,3,0,1],[0,0,0,0]]

In [24]: y = [[el for el in l if el != 0] for l in x]

In [25]: y
Out[25]: [[1, 3], [1, 1, 1, 1], [5, 2], [4, 3, 1], []]

Alternatively, 或者,

In [28]: [filter(None, l) for l in x]
Out[28]: [[1, 3], [1, 1, 1, 1], [5, 2], [4, 3, 1], []]

Or using functools : 或使用functools

In [32]: map(functools.partial(filter, None), x)
Out[32]: [[1, 3], [1, 1, 1, 1], [5, 2], [4, 3, 1], []]

Use list comprehensions : 使用list comprehensions

>>> [[i for i in j if i] for j in x]
[[1, 3], [1, 1, 1, 1], [5, 2], [4, 3, 1], []]

Also, note, that I fell back on the fact, that integers int are promoted to false only when they are equal to 0 , and they are true of they are not equal to 0 . 另外,请注意,我不赞成这样的事实:整数int仅在等于0时才提升为false ,而在不等于0时才为true It is worth contemplating if it would be wise to use if i != 0 instead of if i , as the former is more explicit and thus more Pythonic. 这是值得考虑,如果它是明智的使用if i != 0 ,而不是if i ,因为前者更加明确,从而更符合Python。

You can filter the lists: 您可以filter列表:

y = map(lambda l: filter(None, l), x)

Or you can use partial : 或者您可以使用partial

from functools import partial

y = map(partial(filter, None), x)
x = [[1,0,0,3],[1,1,1,1],[5,2,0,0],[4,3,0,1],[0,0,0,0]]

print x
print 'id(x) ==',id(x)
for sublist in x:
    print map(id,sublist)

for sublist in x:
    for i in xrange(len(sublist)-1,-1,-1):
        if sublist[i]==0:
            del sublist[i]

print
print x
print 'id(x) ==',id(x)
for sublist in x:
    print map(id,sublist)

With this code, you really ELIMINATE the zeros FROM THE INITIAL list without having to create another list, that is to say this code acts in place in x 使用此代码,您实际上消除了INITIAL列表中的零,而无需创建另一个列表,也就是说,此代码在x中就位

The printing of id(x) and map(id,sublist) is done to show this way of modification: 完成id(x)map(id,sublist)的打印以显示这种修改方式:
the identities, that is to say the adresses of the elements of the sublists (for the ones that remain), and of the list x itself, are the same before and after the treatment 在处理之前和之后,标识(即,子列表的元素(对于剩余元素)的地址以及列表x本身的地址)是相同的

If the list is big, it may be interesting to modify it in place. 如果列表很大,那么就地进行修改可能会很有趣。
If it is not the case, you can use the other solutions that all create a new list object. 如果不是这种情况,则可以使用其他所有创建新列表对象的解决方案。

The indexes are run in the reverse sense thanks to xrange(len(sublist)-1,-1,-1) because if the list was explored from left to right, it would give erroneous result 由于使用xrange(len(sublist)-1,-1,-1)索引以相反的方式运行,因为如果从左到右浏览列表,则会产生错误的结果

Result 结果

[[1, 0, 0, 3], [1, 1, 1, 1], [5, 2, 0, 0], [4, 3, 0, 1], [0, 0, 0, 0]]
id(x) == 18725984
[10021864, 10021876, 10021876, 10021840]
[10021864, 10021864, 10021864, 10021864]
[10021816, 10021852, 10021876, 10021876]
[10021828, 10021840, 10021876, 10021864]
[10021876, 10021876, 10021876, 10021876]

[[1, 3], [1, 1, 1, 1], [5, 2], [4, 3, 1], []]
id(x) == 18725984
[10021864, 10021840]
[10021864, 10021864, 10021864, 10021864]
[10021816, 10021852]
[10021828, 10021840, 10021864]
[]

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

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