text - 在 python 中查找许多元素中的第一个 - text.find(a , b , c)

标签 text scripting find python

我想检查“a”中的某个词是否在“文本”中

text = "testing if this works"
a = ['asd' , 'test']
print text.find(a)

我该怎么做?

谢谢

最佳答案

如果你想检查 a 中的任何单词是否在文本中,那么使用 any :

any(word in text  for word in a)

如果您想知道 a 中出现在文本中的单词数,您可以简单地 add他们:

print('Number of words in a that match text: %s' %
      sum(word in text  for word in a))

如果您只想匹配完整的单词(即您不想匹配 test 单词 testing),请将文本拆分为单词,如:

words = set(text.split())
any(word in words  for word in a)

关于text - 在 python 中查找许多元素中的第一个 - text.find(a , b , c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8884543/

相关文章:

git - 逐字的 git 修订

Android 9 应用程序菜单是深色背景上的深色文本

linux - 使用 shell 或 bash 脚本根据目录名称删除多个目录的脚本

regex - 如何在 bash 中替换字符串中的空格和斜杠?

mongodb - 从包含引用的字段获取数据

java - 在Java中使用文本文件携带命令

python - 如何从键值对列表中删除值

ruby - 使用 Ruby FileUtils 时出现运行时错误

arrays - 在 MongoDB 的对象数组中查找匹配多个字段的文档

javascript - JavaScript 中的正则表达式 : Content Between Two Tags