Pythonic方法限制变量的范围?

时间:2021-12-31 21:25:07

Is there a better way to do this?

有一个更好的方法吗?

if a > 1:
    a = 1
if a < 0:
    a = 0

I was thinking of using a function because I have a lot of these in my code, Was curious if there was a shorter comprehensible way to do it.

我正在考虑使用一个函数,因为我的代码中有很多这些函数,很奇怪是否有一个更简单易懂的方法。

2 个解决方案

#1


8  

What you describe is usually called clipping. There are several ways to do clipping.

你描述的通常称为剪裁。有几种方法可以进行裁剪。

Library (numpy)

You can use numpy.clip for that:

您可以使用numpy.clip:

numpy.clip(a, a_min, a_max, out=None)

So:

所以:

import numpy

numpy.clip(x,0,1)

Although since function calls in Python are expensive, and numpy usually processes data in batch, for a single value it will result in computational overhead.

虽然由于Python中的函数调用很昂贵,并且numpy通常批量处理数据,但对于单个值,它将导致计算开销。

For example:

例如:

>>> x = -2.5
>>> numpy.clip(x,0,1)
0.0
>>> x = 2.5
>>> numpy.clip(x,0,1)
1.0
>>> x = 0.5
>>> numpy.clip(x,0,1)
0.5

Usually you use numpy to perform operations on (large) matrices, for instance if you have to process a 1000x1000 matrix, than using numpy will definitely pay off.

通常你使用numpy对(大)矩阵执行操作,例如,如果你必须处理1000x1000矩阵,那么使用numpy肯定会得到回报。

Pure Python

A pure python approach can be obtained with:

可以通过以下方式获得纯python方法:

max(0,min(1,x))

But here you have two calls, as a result, it will be slower than using if statements.

但是这里有两个调用,因此它比使用if语句慢。

Finally if you stick with the if-code, you can optimize it a tiny bit by using an elif:

最后,如果你坚持使用if-code,你可以使用elif优化它:

if x < 0:
    x = 0
elif x > 1:
    x = 1

Or a more generic function:

或者更通用的功能:

def clip(x,min,max):
    if x < min:
        return min
    elif x > max:
        return max
    return x

#2


4  

I'd always use something like max(0, min(1, x)). If x is more than 1, min(1, x) will be 1, and max(0, 1) is still 1. If x is less than 0, min(1, x) will be x, and max(0, x) will be 0.

我总是使用像max(0,min(1,x))这样的东西。如果x大于1,则min(1,x)为1,max(0,1)仍为1.如果x小于0,则min(1,x)为x,max(0, x)将为0。

#1


8  

What you describe is usually called clipping. There are several ways to do clipping.

你描述的通常称为剪裁。有几种方法可以进行裁剪。

Library (numpy)

You can use numpy.clip for that:

您可以使用numpy.clip:

numpy.clip(a, a_min, a_max, out=None)

So:

所以:

import numpy

numpy.clip(x,0,1)

Although since function calls in Python are expensive, and numpy usually processes data in batch, for a single value it will result in computational overhead.

虽然由于Python中的函数调用很昂贵,并且numpy通常批量处理数据,但对于单个值,它将导致计算开销。

For example:

例如:

>>> x = -2.5
>>> numpy.clip(x,0,1)
0.0
>>> x = 2.5
>>> numpy.clip(x,0,1)
1.0
>>> x = 0.5
>>> numpy.clip(x,0,1)
0.5

Usually you use numpy to perform operations on (large) matrices, for instance if you have to process a 1000x1000 matrix, than using numpy will definitely pay off.

通常你使用numpy对(大)矩阵执行操作,例如,如果你必须处理1000x1000矩阵,那么使用numpy肯定会得到回报。

Pure Python

A pure python approach can be obtained with:

可以通过以下方式获得纯python方法:

max(0,min(1,x))

But here you have two calls, as a result, it will be slower than using if statements.

但是这里有两个调用,因此它比使用if语句慢。

Finally if you stick with the if-code, you can optimize it a tiny bit by using an elif:

最后,如果你坚持使用if-code,你可以使用elif优化它:

if x < 0:
    x = 0
elif x > 1:
    x = 1

Or a more generic function:

或者更通用的功能:

def clip(x,min,max):
    if x < min:
        return min
    elif x > max:
        return max
    return x

#2


4  

I'd always use something like max(0, min(1, x)). If x is more than 1, min(1, x) will be 1, and max(0, 1) is still 1. If x is less than 0, min(1, x) will be x, and max(0, x) will be 0.

我总是使用像max(0,min(1,x))这样的东西。如果x大于1,则min(1,x)为1,max(0,1)仍为1.如果x小于0,则min(1,x)为x,max(0, x)将为0。