gpt4 book ai didi

python - 如何搜索单词而不是整个组合

转载 作者:太空宇宙 更新时间:2023-11-04 04:59:16 25 4
gpt4 key购买 nike

    a = ["hi","hel","jhd"]
b = "hhgghihjh"
if(any (x in b for x in a)):
print("found")

上面的结果是真的,因为在另一个词中找到了“hi”这个词。我想让它变成假,因为我只想检查这个词,而不是检查主词中想要的词。我是一个新手到 python。谢谢

最佳答案

您可以使用 == 运算符进行字符串比较。

a = "hi"
b = "hhgghihjh"
if ("x" == a or "x" == b):
print("found")

或者要在字符串中搜索特定键,请使用 in

a = "hi"
b = "hhgghihjh"
if ("x" in a or "x" in b):
print("found")

关于python - 如何搜索单词而不是整个组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com