[英]Python: “if closest to 1”
I am doing some calculations on a dictionary. 我正在对字典进行一些计算。 But the important thing is I want to make a if-condition that kind of says
但是重要的是我想做一个if条件
"if x has a value that is closer to 1 (or equal to 1) than variable " “如果x的值比变量的值更接近1(或等于1),则“
kind of hard to explain, but hope you understand. 有点难以解释,但希望您能理解。
You can use absolute value : 您可以使用绝对值 :
if abs(x-1) < abs(variable-1):
...
since absolute value of x-1
is the distance between x
and 1
, and similarly absolute value of variable-1
is the distance between variable
and 1
. 因为
x-1
绝对值是x
与1
之间的距离,并且类似地, variable-1
绝对值是variable
与1
之间的距离。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.