python - 检查列表中的任何字符串是否包含在另一个列表中的任何字符串中

标签 python string list substring

我有一个新闻标题列表,想检查是否有任何标题包含列表中的任何关键字。

例如

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/

相关文章:

Python - 为什么这段代码被认为是一个生成器?

python - 如何在 jupyter notebook 中使用 NOT 运算符 ~?

python - 如何在Python中读取文档的特定部分

python - scrapy 蜘蛛不返回任何结果

Java - 将字符串拆分为具有字符限制的句子

python - 如何让python在将字符串写入文件时使用双引号

java - 如何将运行时已知的值 ('how many' 传递给 SQL 查询

python - Django ManyToMany 模板问题

string - 算法:将较小的字符串最佳地组合在一起以生成指定长度的较长字符串?

Java使用List存储List