检查python中的多变量是否大于零 A=200 B=-140 C=400 D=-260 如果A>>b=V(100) >>>a-b#通常50-100等于-50,不是吗? 0 (在[A,B,C,D]中x的最大值(x,0))的可读性要高得多。@mgilson我同意-并在我的编辑中进行了更改,还有一些解释和链接。值得注意的是,CapWords通常是为Python中的类保留的-查看更多信息。我很确定Python社区会建议不要使用模糊的变量名,如A、B、C、,D无论如何:^)。+1更喜欢使用map和lambda来完成列表理解所做的事情。 values = [200, -140, 400, -260] values = [max(x, 0) for x in values] >>> class V(int): ... def __new__(cls,val,**kwargs): ... return super(V,cls).__new__(cls,max(val,0)) >>> A=V(200) 200 >>> B=V(-140) 0 >>> [V(i) for i in [200, -140, 400, -260]] [200, 0, 400, 0] >>> A,B,C,D = [V(i) for i in [200, -140, 400, -260]] >>> class V(int): ... def __new__(cls,val,**kwargs): ... return super(V,cls).__new__(cls,max(val,0)) ... def __sub__(self,other): ... if int.__sub__(self,other)<0: ... return 0 ... else: ... return int.__sub__(self,other) >>> a=V(50) >>> b=V(100) >>> a-b #ordinarily 50-100 would be -50, no? 0

检查python中的多变量是否大于零 A=200 B=-140 C=400 D=-260 如果A>>b=V(100) >>>a-b#通常50-100等于-50,不是吗? 0 (在[A,B,C,D]中x的最大值(x,0))的可读性要高得多。@mgilson我同意-并在我的编辑中进行了更改,还有一些解释和链接。值得注意的是,CapWords通常是为Python中的类保留的-查看更多信息。我很确定Python社区会建议不要使用模糊的变量名,如A、B、C、,D无论如何:^)。+1更喜欢使用map和lambda来完成列表理解所做的事情。 values = [200, -140, 400, -260] values = [max(x, 0) for x in values] >>> class V(int): ... def __new__(cls,val,**kwargs): ... return super(V,cls).__new__(cls,max(val,0)) >>> A=V(200) 200 >>> B=V(-140) 0 >>> [V(i) for i in [200, -140, 400, -260]] [200, 0, 400, 0] >>> A,B,C,D = [V(i) for i in [200, -140, 400, -260]] >>> class V(int): ... def __new__(cls,val,**kwargs): ... return super(V,cls).__new__(cls,max(val,0)) ... def __sub__(self,other): ... if int.__sub__(self,other)<0: ... return 0 ... else: ... return int.__sub__(self,other) >>> a=V(50) >>> b=V(100) >>> a-b #ordinarily 50-100 would be -50, no? 0,python,Python,上述代码结构的简写实现是什么。? 有没有更好的/优雅的/方便的方法来实现这一点?这可以通过使用和解包来轻松解决 另一种解决方案是使用map()函数,但是可读性较差: v1, v2, v3, v4 = [max(x, 0) for x in [a, b, c, d]] 使用min()和max()检查是否正常 如果您更愿意使用max函数来处理python三元运算符,它将如下所示: A = 200 B = -140 C = 400 D = -260 v1, v2, v3, v4 = [x if x

上述代码结构的简写实现是什么。?
有没有更好的/优雅的/方便的方法来实现这一点?

这可以通过使用和解包来轻松解决

另一种解决方案是使用
map()
函数,但是可读性较差:

v1, v2, v3, v4 = [max(x, 0) for x in [a, b, c, d]]

使用
min()
max()
检查是否正常

如果您更愿意使用
max
函数来处理python三元运算符,它将如下所示:

A = 200
B = -140
C = 400
D = -260

v1, v2, v3, v4 = [x if x > 0 else 0 for x in (A, B, C, D)]

但是,如果你打算让所有这些变量都处理相同,你可能首先考虑把它们放在列表/元组中。

v1, v2, v3, v4 = [max(x, 0) for x in (A, B, C, D)]
作为另一种选择,您可以将
int
子类化为自定义类来实现这一点

values = [200, -140, 400, -260]
values = [max(x, 0) for x in values]
然后直接使用它:

>>> class V(int):
...    def __new__(cls,val,**kwargs):
...       return super(V,cls).__new__(cls,max(val,0))
这样做的唯一好处是,您可以适当地覆盖
\uuuuu sub\uuuuuuuuuu
\uuuuuuu add\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,然后您的输入将始终大于0,即使您有
a=V(50)-100

例如:

>>> A=V(200)
200
>>> B=V(-140)
0
>>> [V(i) for i in [200, -140, 400, -260]]
[200, 0, 400, 0]     
>>> A,B,C,D = [V(i) for i in [200, -140, 400, -260]]
>第五类(内部):
...    定义新(cls,val,**kwargs):
...       返回超级值(V,cls)。\新值(cls,最大值(val,0))
...    定义(自身、其他):
...       如果int.uuu sub_uuu(自身、其他)>>a=V(50)
>>>b=V(100)
>>>a-b#通常50-100等于-50,不是吗?
0

(在[A,B,C,D]中x的最大值(x,0))
的可读性要高得多。@mgilson我同意-并在我的编辑中进行了更改,还有一些解释和链接。值得注意的是,
CapWords
通常是为Python中的类保留的-查看更多信息。我很确定Python社区会建议不要使用模糊的变量名,如
A、B、C、,D
无论如何:^)。+1更喜欢使用
map
lambda
来完成列表理解所做的事情。
values = [200, -140, 400, -260]
values = [max(x, 0) for x in values]
>>> class V(int):
...    def __new__(cls,val,**kwargs):
...       return super(V,cls).__new__(cls,max(val,0))
>>> A=V(200)
200
>>> B=V(-140)
0
>>> [V(i) for i in [200, -140, 400, -260]]
[200, 0, 400, 0]     
>>> A,B,C,D = [V(i) for i in [200, -140, 400, -260]]
>>> class V(int):
...    def __new__(cls,val,**kwargs):
...       return super(V,cls).__new__(cls,max(val,0)) 
...    def __sub__(self,other):
...       if int.__sub__(self,other)<0:
...         return 0
...       else:
...         return int.__sub__(self,other) 
>>> a=V(50)
>>> b=V(100)
>>> a-b                #ordinarily 50-100 would be -50, no?
0