假设我有一个列表
test = ["a","bb","ph","phi","phi_ph"]
其中test
的成员可以包含字符串ph
,字符串phi
,这两者的组合,或者不包含任何字符串ph
。如何过滤此列表以仅保留包含test_filtered = ["ph","phi_ph"]
的元素,以便:
[x for x in test if 'ph' in x]
例如
>> ["ph","phi","phi_ph"]
返回
Calendar
答案 0 :(得分:1)
使用re.compile()
和re.search()
函数的解决方案:
import re
test = ["a","bb","ph","phi","phi_ph", "phi_abc", "ph_a"]
search_str = 'ph'
pattern = re.compile(r'(^|[^a-z0-9])'+ search_str + '([^a-z0-9]|$)')
result = [i for i in test if re.search(pattern, i)]
print(result)
输出:
['ph', 'phi_ph', 'ph_a']
(^|[^a-z0-9])
- 交替组,确保搜索字符串(即ph
)应出现在字符串的开头或以非字母数字字符开头
([^a-z0-9]|$)
- 交替组,确保搜索字符串(即ph
)应出现在字符串末尾或后跟非字母数字字符
答案 1 :(得分:0)
这应该照顾它:
test = ["a","bb","ph","phi","phi_ph"]
result = [y for y in test for x in y.split('_') if x == "ph"]
这样您只需添加"ph"
,而且不需要排除任何内容。
答案 2 :(得分:0)
我发现以下列表理解完成了这项工作:
test_filtered = [x for x in test if ('ph' in x and x.count('ph')!=x.count('phi'))]
>> ['ph', 'phi_ph']
答案 3 :(得分:0)
试试这个:
In [26]: new_filter=[ x.split('_')[0] for x in test_filtered]+test_filtered
In [27]: [x for x in test if x in new_filter]
Out[27]: ['ph', 'phi', 'phi_ph']