如何在Python中的字典值中搜索多个字符串?

问题描述 投票:0回答:1

我想匹配Python字典中的多个字符串,该字典通常只有1个键值对。我正在寻找最优化的代码,例如 1 行代码。 目前我正在尝试使用以下代码,但即使它有效,我也在寻找 1 衬垫

a = "increment"
b = "variable"
c = "found"
s = {'d':'want to increment the location variable based on which x is found'}

if any(a,b,c in value for value in s.values()):
    print("YES")
else:
    print("NO")
python string dictionary search
1个回答
0
投票

只需打印条件结果:

print(any(a,b,c in value for value in s.values()))

这将打印

True
False

如果您确实想要

YES
NO
,请使用条件表达式:

print('YES' if any(a,b,c in value for value in s.values()) else 'NO')
© www.soinside.com 2019 - 2024. All rights reserved.