以下是我需要做的:
if ((a<100 and a>-100) and (b<100 and b>-100)):
#i.e., if both a and b lie in the interval (-100,100)
想知道我是否可以用更短的方式写这个。
UPD:通过以下方式找到。还有比这更短的吗? (虽然这对于只有 2 个变量来说并不算短)
if all((x > -100 and x < 100) for x in (a,b))
请您参考如下方法:
你可以写
(a<100 and a>-100)
作为
-100 < a < 100
因此您可以将表达式简化为
if -100 < a < 100 and -100 < b < 100:
#Do things
因为你的限制是关于零对称的,进一步的简化可能是使用:
abs(a) < 100
