3 回答
TA贡献1827条经验 获得超8个赞
您可以使用集合来找出用户输入的字符串与您的关键字之间匹配的字符串。
检查以下代码:
keywords= ["freeway", "doesn't turn on", "dropped", "got sick", "traffic jam", " car accident"]
user_strings = []
while True:
x = input("Enter a string?")
if x == 'exit':
break
user_strings.append(x)
print ("User strings = %s" %(user_strings))
print ("keywords = %s" %(keywords))
print ("Matched Words = %s" %(list(set(keywords) & set(user_strings))))
输出:
Enter a string?"doesn't turn on"
Enter a string?"freeway"
Enter a string?"Hello"
Enter a string?"World"
Enter a string?"exit"
User strings = ["doesn't turn on", 'freeway', 'Hello', 'World']
keywords = ['freeway', "doesn't turn on", 'dropped', 'got sick', 'traffic jam', ' car accident']
Matched Words = ['freeway', "doesn't turn on"]
添加回答
举报