son*_*089 6 python 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']
Run Code Online (Sandbox Code Playgroud)
但是有没有办法返回
['a b self','foo c','b', 'c', 'b', 'd', 'd']
Run Code Online (Sandbox Code Playgroud)
由于 b 和 c 包含在'a b self'
and 中'foo c'
,我希望代码也返回这两个。
假设代码需要检索包含任何条件字符串的所有字符串:
[elem for elem in li if any(c in elem for c in condition)]
Run Code Online (Sandbox Code Playgroud)
如果需要完全匹配条件:
[elem for elem in li if
any(re.search('(^|\s){}(\s|$)'.format(c), elem) for c in condition)]
Run Code Online (Sandbox Code Playgroud)
编辑:这可以简化为单个预定义的正则表达式:
predicate = re.compile('(^|\s)({})(\s|$)'.format('|'.join(condition)))
[elem for elem in li if predicate.search(elem)]
Run Code Online (Sandbox Code Playgroud)