如何解决我有一个关键字列表,我希望能够找到一个字符串是否包含这些关键字中的任何一个
.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
如果还有什么,请写在评论中。否则,如果您接受我的解决方案,我将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。