如何将数字限制在指定范围内? (蟒蛇)

时间:2021-03-31 22:54:01

I want to limit a number to be within a certain range. Currently, I am doing the following:

我想将数字限制在一定范围内。目前,我正在做以下事情:

minN = 1
maxN = 10
n = something() #some return value from a function
n = max(minN, n)
n = min(maxN, n)

This keeps it within minN and maxN, but it doesn't look very nice. How could I do it better?

这使它保持在minN和maxN之内,但它看起来不太好。我怎么能做得更好?

PS: FYI, I am using Python 2.6.

PS:仅供参考,我使用的是Python 2.6。

5 个解决方案

#1


44  

def clamp(n, minn, maxn):
    return max(min(maxn, n), minn)

or functionally equivalent:

或功能相当:

clamp = lambda n, minn, maxn: max(min(maxn, n), minn)

now, you use:

现在,您使用:

n = clamp(n, 7, 42)

or make it perfectly clear:

或者说得非常清楚:

n = minn if n < minn else maxn if n > maxn else n

even clearer:

更清楚:

def clamp(n, minn, maxn):
    if n < minn:
        return minn
    elif n > maxn:
        return maxn
    else:
        return n

#2


37  

If you want to be cute, you can do:

如果你想变得可爱,你可以这样做:

n = sorted([minN, n, maxN])[1]

#3


33  

Simply use numpy.clip() (doc):

只需使用numpy.clip()(doc):

n = np.clip(n, minN, maxN)

It also works for whole arrays:

它也适用于整个数组:

my_array = np.clip(my_array, minN, maxN)

#4


2  

Define a class and have a method for setting the value which performs those validations.

定义一个类,并有一个方法来设置执行这些验证的值。

Something vaguely like the below:

有点像下面的东西:

class BoundedNumber(object):
    def __init__(self, value, min_=1, max_=10):
        self.min_ = min_
        self.max_ = max_
        self.set(value)

    def set(self, newValue):
        self.n = max(self.min_, min(self.max_, newValue))

# usage

bounded = BoundedNumber(something())
bounded.set(someOtherThing())

bounded2 = BoundedNumber(someValue(), min_=8, max_=10)
bounded2.set(5)    # bounded2.n = 8

#5


0  

Could you not string together some one-line python conditional statements?

I came across this question when looking for a way to limit pixel values between 0 and 255, and didn't think that using max() and min() was very readable so wrote the following function:

我在寻找一种限制像素值在0到255之间的方法时遇到了这个问题,并且没想到使用max()和min()是非常可读的,所以编写了以下函数:

def clamp(x, minn, maxx):
   return x if x > minm and x < maxx else (minn if x < minn else maxx)

I would be interested to see how someone more experienced than me would find this way of clamping a value. I assume it must be less efficient than using min() and max(), but it may be useful for someone looking for a more readable (to me at least) function.

我有兴趣看到一个比我更有经验的人会发现这种方法能够锁定价值。我认为它必须比使用min()和max()效率低,但它对于寻找更易读(至少对我来说)功能的人来说可能是有用的。

#1


44  

def clamp(n, minn, maxn):
    return max(min(maxn, n), minn)

or functionally equivalent:

或功能相当:

clamp = lambda n, minn, maxn: max(min(maxn, n), minn)

now, you use:

现在,您使用:

n = clamp(n, 7, 42)

or make it perfectly clear:

或者说得非常清楚:

n = minn if n < minn else maxn if n > maxn else n

even clearer:

更清楚:

def clamp(n, minn, maxn):
    if n < minn:
        return minn
    elif n > maxn:
        return maxn
    else:
        return n

#2


37  

If you want to be cute, you can do:

如果你想变得可爱,你可以这样做:

n = sorted([minN, n, maxN])[1]

#3


33  

Simply use numpy.clip() (doc):

只需使用numpy.clip()(doc):

n = np.clip(n, minN, maxN)

It also works for whole arrays:

它也适用于整个数组:

my_array = np.clip(my_array, minN, maxN)

#4


2  

Define a class and have a method for setting the value which performs those validations.

定义一个类,并有一个方法来设置执行这些验证的值。

Something vaguely like the below:

有点像下面的东西:

class BoundedNumber(object):
    def __init__(self, value, min_=1, max_=10):
        self.min_ = min_
        self.max_ = max_
        self.set(value)

    def set(self, newValue):
        self.n = max(self.min_, min(self.max_, newValue))

# usage

bounded = BoundedNumber(something())
bounded.set(someOtherThing())

bounded2 = BoundedNumber(someValue(), min_=8, max_=10)
bounded2.set(5)    # bounded2.n = 8

#5


0  

Could you not string together some one-line python conditional statements?

I came across this question when looking for a way to limit pixel values between 0 and 255, and didn't think that using max() and min() was very readable so wrote the following function:

我在寻找一种限制像素值在0到255之间的方法时遇到了这个问题,并且没想到使用max()和min()是非常可读的,所以编写了以下函数:

def clamp(x, minn, maxx):
   return x if x > minm and x < maxx else (minn if x < minn else maxx)

I would be interested to see how someone more experienced than me would find this way of clamping a value. I assume it must be less efficient than using min() and max(), but it may be useful for someone looking for a more readable (to me at least) function.

我有兴趣看到一个比我更有经验的人会发现这种方法能够锁定价值。我认为它必须比使用min()和max()效率低,但它对于寻找更易读(至少对我来说)功能的人来说可能是有用的。