python,从列表中删除字符串中的单词

时间:2014-11-25 10:26:48

标签: python string list

我想删除字符串中的单词,即列表中的单词或“种子”单词,

示例:

query = "LK936033.1 Babesia assembly 454hybrid_PBjelly scaffold Contig1323  7"
seeds = ["assembly","454","scaffold","contig"]

期待结果:

"LK936033.1 Babesia 7"

我找到了一种删除这样的词的方法:

' '.join([i for i in query.split() if i not in seeds])

但此方法仅从种子列表中删除完全单词,但不删除包含种子的单词。

1 个答案:

答案 0 :(得分:2)

你需要扩展你的考试;使用any() function提高效率:

' '.join([i for i in query.split() if not any(w in i.lower() for w in seeds)])

any(w in i.lower() for w in seeds)测试使用生成器表达式来测试当前单词是否包含任何种子词,小写;它只会评估找到匹配项的最小测试次数。

演示:

>>> query = "LK936033.1 Babesia assembly 454hybrid_PBjelly scaffold Contig1323  7"
>>> seeds = ["assembly","454","scaffold","contig"]
>>> ' '.join([i for i in query.split() if not any(w in i.lower() for w in seeds)])
'LK936033.1 Babesia 7'