terms = ["dog","cat","fish"]
texts = ["I like my dog", "Hello world", "Random text"]
results = ["match" if a in b else "no match" for a,b in zip(terms,texts)]
print(results)
输出:
['match', 'no match', 'no match']
更新:原来拉链不是OP想要的。
terms = ["dog","cat","fish"]
texts = ["I like my dog", "Hello world", "Random text"]
results = ["match" if any(b in a for b in terms) else "no match" for a in texts]
print(results)
下面是如何使用
zip()
和列表理解:输出:
更新:原来拉链不是OP想要的。
输出:
相关问题 更多 >
编程相关推荐