【问题标题】:im trying to check if my string contains certain words what is wrong? PYTHON [duplicate]我试图检查我的字符串是否包含某些单词有什么问题?蟒蛇[重复]
【发布时间】:2015-02-08 01:22:53
【问题描述】:

我正在使用 Python 来尝试检查字符串是否包含某些单词。它可以包含全部或部分单词。

listCall = ('Azura', 'Fly', 'yellow')
readME = 'the color is yellow'
if listCall in readME:
    print 'we found certain words'
else:
    print 'all good'

【问题讨论】:

标签: python string list tuples operand


【解决方案1】:

您正在测试 *whole list listCall* is inreadME`。您需要改为测试单个词

if any(word in readME for word in listCall):

这使用生成器表达式和any() function 来针对readME 字符串有效地测试listCall 中的单个单词。

【讨论】:

  • 谢谢!它对我的帮助比你认真思考的要大。
猜你喜欢
  • 2015-03-14
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2018-05-01
  • 2021-09-10
  • 1970-01-01
相关资源
最近更新 更多