我想检查 Python 数组的每个元素中是否包含“car”和“motorbike”这两个词。我知道如何使用 in 检查一个单词,但不知道如何使用 2 个单词。非常感谢任何帮助
请您参考如下方法:
两个词的解决方案:
for string in array:
if 'car' in string and 'motorbike' in string.split():
print("Car and motorbike are in string")
检查 test_words 中的所有 单词是否在 string 中的 n-word 解决方案:
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")
