【问题标题】:How to compare a string with an array in python? [duplicate]如何在python中比较字符串和数组? [复制]
【发布时间】:2023-02-05 17:25:50
【问题描述】:

任务是检查字符串中是否存在数组中的单词。

str = 'Dear friends, the new model of organizational activity contributes to the preparation and implementation of the personnel training system.'

stop_words = [ 'training', 'moonshine']


if all(stop_words in str):
    print('Find stop word')
else:
    print('All goods, no stop words')

这里python说他比不了。请告诉我有哪些解决方案?

【问题讨论】:

  • “他不能编辑”什么?
  • 对不起“比较”

标签: python arrays python-3.x string


【解决方案1】:

你有以下错误

TypeError: 'in <string>' requires string as left operand, not list

由于您不想将 list 搜索到 str,这没有任何意义,您只能将一个字符串搜索到另一个字符串中,但您必须对列表中的每个字符串执行此操作


关于你的字符串no stop words,我想你想要以下,你想要任何(不是每个)wordstop_wordsvalue

value = 'Dear friends, the new model of organizational activity contributes to the preparation and implementation of the personnel training system.'
stop_words = [ 'training', 'moonshine']

if any(word in value for word in stop_words):
    print('Find a stop word')
else:
    print('All goods, no stop words')

【讨论】:

  • 更加感谢!这对我有用!
猜你喜欢
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 2012-11-04
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多