简体   繁体   English

Python 3:从元组列表中删除空元组

[英]Python 3: Removing an empty tuple from a list of tuples

I have a list of tuples that reads as such: 我有一个这样的元组列表:

>>>myList
[(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)]

And I would like it to read as such: 我希望这样读:

>>>myList
[('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)]

ie I would like to remove the empty tuples () from the list. 即我想从列表中删除空的元组() While doing this I want to preserve the tuple ('',) . 在执行此操作时,我想保留元组('',) I cannot seem to find a way to remove these empty tuples from the list. 我似乎找不到从列表中删除这些空元组的方法。

I have tried myList.remove(()) and using a for loop to do it, but either that doesn't work or I am getting the syntax wrong. 我已经尝试过myList.remove(())并使用for循环来做到这一点,但是那还是行不通或者我弄错了语法。 Any help would be appreciated. 任何帮助,将不胜感激。

You can filter 'empty' values: 您可以过滤“空”值:

filter(None, myList)

or you can use a list comprehension. 或者您可以使用列表理解。 On Python 3, filter() returns a generator; 在Python 3上, filter()返回一个生成器; the list comprehension returns a list on either Python 2 or 3: list comprehension返回有关Python 2或3的列表:

[t for t in myList if t]

If your list contains more than just tuples, you could test for empty tuples explicitly: 如果列表中不仅包含元组,还可以显式测试空元组:

[t for t in myList if t != ()]

Python 2 demo: Python 2演示:

>>> myList = [(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)]
>>> filter(None, myList)
[('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)]
>>> [t for t in myList if t]
[('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)]
>>> [t for t in myList if t != ()]
[('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)]

Of these options, the filter() function is fastest: 在这些选项中, filter()函数最快:

>>> timeit.timeit('filter(None, myList)', 'from __main__ import myList')
0.637274980545044
>>> timeit.timeit('[t for t in myList if t]', 'from __main__ import myList')
1.243359088897705
>>> timeit.timeit('[t for t in myList if t != ()]', 'from __main__ import myList')
1.4746298789978027

On Python 3, stick to the list comprehension instead: 在Python 3上,请坚持使用列表理解:

>>> timeit.timeit('list(filter(None, myList))', 'from __main__ import myList')
1.5365421772003174
>>> timeit.timeit('[t for t in myList if t]', 'from __main__ import myList')
1.29734206199646
myList = [x for x in myList if x != ()]

Use a list comprehension to filter out the empty tuples: 使用列表推导来过滤出空的元组:

>>> myList = [(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)]
>>> myList = [x for x in myList if x]
>>> myList
[('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)]
>>>

This works because empty tuples evaluate to False in Python. 之所以可行,是因为在Python中空元组的值为False

Explicit is better than implicit 显式胜于隐式

I find this one is more readable and not ambiguous by specifying clearly what function of the filter is. 通过明确指定过滤器的功能,我发现此代码更具可读性,并且不模糊。 So clearly we want to remove those empty tuple which is () . 所以很明显,我们想删除那些空的元组()

def filter_empty_tuple(my_tuple_list):
    return filter(lambda x: x != (), my_tuple_list)

# convert to list
def filter_empty_tuple_to_list(my_tuple_list):
    return list(filter(lambda x: x != (), my_tuple_list))

Perhaps it would be good if you don't convert them into a list and use it as generator . 如果您不将它们转换为list并将其用作generator则可能会很好。 See this question when deciding which to use 在决定使用哪个时请参阅此问题

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

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