如何检查至少一个变量是否具有特定值

时间:2019-07-18 17:27:36

标签: python variables if-statement

有没有一种捷径可以在python中编写以下表达式?

我正在检查是否至少有一个变量的值为“ None”

谢谢

if hBorder1!="None" or hBorder2!="None" or vBorder1!="None" or vBorder2 !="None":
        return "BorderByType"
    else:
        return ""

3 个答案:

答案 0 :(得分:4)

if any(b!="None" for b in (hBorder1, hBorder2, vBorder1, vBorder2)):
        return "BorderByType"
    else:
        return ""

或者...

return ('', "BorderByType")[int(any(b!="None" for b in (hBorder1, hBorder2, vBorder1, vBorder2)))]

答案 1 :(得分:1)

if "None" in (hBorder1, hBorder2, vBorder1, vBorder2):
    ...

这将在第一个“ None”值处短路。

答案 2 :(得分:0)

if "None" not in [hBorder1, hBorder2, vBorder1, vBorder2]
    return "BorderByType"
else:
    return "

return "BorderByType" if "None" not in [hBorder1, hBorder2, vBorder1, vBorder2] else ''