我想检查Python数组的每个元素中是否有两个单词“汽车”和“摩托车”。我知道如何检查一个单词in
但不知道如何用两个词来做。非常感谢任何帮助
两字解决方案:
for string in array:
if 'car' in string and 'motorbike' in string.split():
print("Car and motorbike are in string")
n 个字的解决方案来检查是否all中的单词test_words
are in string
:
test_words = ['car', 'motorbike']
contains_all = True
for string in array:
for test_word in test_words:
if test_word not in string.split()::
contains_all = False
break
if not contains_all:
break
if contains_all:
print("All words in each string")
else:
print("Not all words in each string")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)