保留列表中的一些元素

时间:2022-10-06 07:41:46

I have been trying to figure out how to find specific words in a list then move these to a new list. Say i have :

我一直试图弄清楚如何在列表中找到特定的单词然后将它们移动到新的列表。说我有:

a = ['a','b','c','d','e']

and I only want to have a, c and e. Is it possible to do this without using .remove() ?

我只想要一个,c和e。是否可以在不使用.remove()的情况下执行此操作?

2 个解决方案

#1


4  

Well, I hope I understood your question right. You want to only select the contents of one list if they are in another list. You might use list comprehensions.

好吧,我希望我能理解你的问题。您只想选择一个列表中的内容(如果它们在另一个列表中)。您可以使用列表推导。

Here's an example playing at the interactive prompt.

这是在交互式提示下播放的示例。

>>> a = ['a','b','c','d','e']
>>> [x for x in a if x in ['a', 'c', 'e' ]]
['a', 'c', 'e']

or with clearer naming

或者更清晰的命名

>>> list = ['a','b','c','d','e', 'a', 'd', 'e', 'c']
>>> wanted = ['a', 'c', 'e' ]
>>> [x for x in list if x in wanted]
['a', 'c', 'e', 'a', 'e', 'c']

#2


3  

Use the filter() builtin function:

使用filter()内置函数:

>>> lst = ['a','b','c','d','e']
>>>
>>> def keep(item):
...     # Implement whatever criterion you need here
...     return item in ('a', 'c', 'e')
...
>>> filter(keep, lst)
['a', 'c', 'e']

filter will simply return a filtered sequence (a list in your case) without modifying the input sequence.

filter只会返回一个已过滤的序列(在您的情况下为一个列表)而不修改输入序列。

keep in this example just needs to be a function that, given an item, decides whether to keep it or not by returning True (keep) or False (discard).

保持这个例子只需要是一个函数,给定一个项目,通过返回True(保持)或False(丢弃)来决定是否保留它。


Note: As pointed out by @tobias_k, in Python 3 filter() will return an iterator. So if you actually do need a list (not just an iterable), you'd need to pass it to the list() constructor like this:

注意:正如@tobias_k所指出的,在Python 3中,filter()将返回一个迭代器。因此,如果您确实需要一个列表(不仅仅是一个可迭代的),您需要将它传递给list()构造函数,如下所示:

newlist = list(filter(keep, lst))

#1


4  

Well, I hope I understood your question right. You want to only select the contents of one list if they are in another list. You might use list comprehensions.

好吧,我希望我能理解你的问题。您只想选择一个列表中的内容(如果它们在另一个列表中)。您可以使用列表推导。

Here's an example playing at the interactive prompt.

这是在交互式提示下播放的示例。

>>> a = ['a','b','c','d','e']
>>> [x for x in a if x in ['a', 'c', 'e' ]]
['a', 'c', 'e']

or with clearer naming

或者更清晰的命名

>>> list = ['a','b','c','d','e', 'a', 'd', 'e', 'c']
>>> wanted = ['a', 'c', 'e' ]
>>> [x for x in list if x in wanted]
['a', 'c', 'e', 'a', 'e', 'c']

#2


3  

Use the filter() builtin function:

使用filter()内置函数:

>>> lst = ['a','b','c','d','e']
>>>
>>> def keep(item):
...     # Implement whatever criterion you need here
...     return item in ('a', 'c', 'e')
...
>>> filter(keep, lst)
['a', 'c', 'e']

filter will simply return a filtered sequence (a list in your case) without modifying the input sequence.

filter只会返回一个已过滤的序列(在您的情况下为一个列表)而不修改输入序列。

keep in this example just needs to be a function that, given an item, decides whether to keep it or not by returning True (keep) or False (discard).

保持这个例子只需要是一个函数,给定一个项目,通过返回True(保持)或False(丢弃)来决定是否保留它。


Note: As pointed out by @tobias_k, in Python 3 filter() will return an iterator. So if you actually do need a list (not just an iterable), you'd need to pass it to the list() constructor like this:

注意:正如@tobias_k所指出的,在Python 3中,filter()将返回一个迭代器。因此,如果您确实需要一个列表(不仅仅是一个可迭代的),您需要将它传递给list()构造函数,如下所示:

newlist = list(filter(keep, lst))