简体   繁体   中英

Filter list of strings using list comprehension

>>> 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 'ab self' and 'foo c' , I want the code to return the two as well.

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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