我想匹配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")
只需打印条件结果:
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')