我有一个新闻标题列表,想检查是否有任何标题包含列表中的任何关键字。
例如
newstitle =['Python is awesome', 'apple is tasty', 'Tom cruise has new movie']
tag = ['Python','Orange', 'android']
如果 tag
中的任何关键字位于 newstitle
中,我希望它返回 True
。
我知道如何使用单个标签来做到这一点:
any('Python' in x for x in newstitle)
但是如何使用多个关键字来做到这一点?
最佳答案
下面的代码应该达到要求:
any(t in x for x in newstitle for t in tag)
来自docs :
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.
关于python - 检查列表中的任何字符串是否包含在另一个列表中的任何字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61428078/