【发布时间】:2020-03-02 05:34:54
【问题描述】:
我有一个字符串和一个定义如下的列表
my_string = 'she said he replied'
my_list = ['This is a cool sentence', 'This is another sentence','she said hello he replied goodbye', 'she replied', 'Some more sentences in here', 'et cetera et cetera...']
我正在尝试检查my_string 中的至少 3 个单词是否存在于my_list 中的任何字符串中。我采用的方法是拆分my_string,并使用all 进行匹配。但是,这只有在my_string 中的所有项目都存在于my_list 的句子中时才有效
if all(word in item for item in my_list for word in my_string.split()):
print('we happy')
1-如果句子列表中至少存在3个my_string,我怎样才能满足条件?
2- 是否可以以相同的顺序仅匹配my_string 中的第一个和最后一个单词?即“she”和“replied”出现在my_list的索引3处的“她回复”中,返回True。
【问题讨论】:
标签: python string list string-matching