Filter list of strings using list comprehension

若如初见. 提交于 2020-01-30 05:18:13

问题


>>> li = ["a b self", "mpilgrim", "foo c", "b", "c", "b", "d", "d"]
>>> condition = ["b", "c", "d"]
>>> [elem for elem in li if elem in condition]
['b', 'c', 'b', 'd', 'd']

But is there a way to return

['a b self','foo c','b', 'c', 'b', 'd', 'd']

Since b and c are included in 'a b self' and 'foo c', I want the code to return the two as well.


回答1:


Assuming the code needs to retrieve all the strings that contain any of the conditions strings:

[elem for elem in li if any(c in elem for c in condition)]

In case a full match of a condition is required:

[elem for elem in li if
 any(re.search('(^|\s){}(\s|$)'.format(c), elem) for c in condition)]

Edit: This can be simplified to a single pre-defined regex:

predicate = re.compile('(^|\s)({})(\s|$)'.format('|'.join(condition)))

[elem for elem in li if predicate.search(elem)]


来源:https://stackoverflow.com/questions/43696958/filter-list-of-strings-using-list-comprehension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!