我有一个关键字列表,我希望能够找到一个字符串是否包含这些关键字中的任何一个

问题描述

.e.关键字 = "cat","hat","mat","bat","fat","sat","rat","pat","foo bar","foo-bar" String = "盒子里有一只猫。" 然后如果字符串包含那个词,那么它应该打印动物否则不是动物。 使用蟒蛇

解决方法

这是您的代码:

Keywords = ["cat","hat","mat","bat","fat","sat","rat","pat","foo bar","foo-bar" ]
String = "There is a mat in the box."
count1=0
count2=0
for i in range(len(Keywords)):
    if Keywords[i] in String:
        print("animal")
        count2+=1
        break
    else:
        count1+=1
if count1>0 and count2==0:
    print("not animal")
,
x = 0
string = "There is a cat in the box."
keywords = ["cat","foo-bar"]
while x < len(keywords):
    if keywords[x] in string: 
        print('There is a ' + keywords[x] + ' in the string')
        x += 1

如果还有什么,请写在评论中。否则,如果您接受我的解决方案,我将不胜感激。