如果数字介于两个值之间,则返回 [Python]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26624095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:46:37  来源:igfitidea点击:

Return if a number is between two values [Python]

pythonpython-3.x

提问by Tanishq dubey

Quite a simple question, but I have no clue on how to implement this.

相当简单的问题,但我不知道如何实现这一点。

Essentially:

本质上:

>>> r = range(4,-1)
>>> 3 in r
False
>>> q = range(-1,4)
>>> 3 in q
True

As you can see, I have the same bounds, -1 and 4, and the same test value, so how do I say that '3' is between'-1' and '4' when I do not know the order that they are given to me in?

如您所见,我有相同的边界,-1 和 4,以及相同的测试值,所以当我不知道它们的顺序时,我怎么说 '3' 是介于 '-1' 和 '4' 之间都给我了?

采纳答案by John La Rooy

Why not sort the bounds first?

为什么不先对边界进行排序?

r = range(*sorted((4, -1)))
q = range(*sorted((-1, 4)))

回答by 101

rangedoesn't do what you think it does.

range不会做你认为它会做的事情。

It creates a list; it's not a numeric range. Actually it does in Python 3.

它创建一个列表;它不是一个数字范围。实际上它在 Python 3 中确实如此。

Just imagine a case, when the lowerEndis -20000 and the upperEndis +20000. Using the range( -20000, 20000 )and comparing numin such a range()is a waste of both memory and CPU-power.

想象一个情况,当lowerEnd是 -20000 和upperEnd是 +20000 时。在这种情况下使用range( -20000, 20000 )和 比较会浪费内存和 CPU 能力。numrange()

It is quite enough to compare a numagainst lowerEnd& upperEndlimit

比较一个numlowerEndupperEnd限制就足够了

You want to check:

您要检查:

num = 3
print(-1 < num < 4)

回答by d-coder

Why don't you try printing each step of your code snippet ? The code becomes self-explanatory to an extent. range()returns a list actually. So when this line is executed

为什么不尝试打印代码片段的每个步骤?代码在某种程度上变得一目了然。range()实际上返回一个列表。所以当这一行被执行时

r = range(4,-1)
print r 

[]   ## returned an empty list so which is why 3 in r returns False.

But when you execute like this

但是当你这样执行时

q = range(-1,4)
print q

[-1, 0, 1, 2, 3]  ## Returned a list with 3 in it so 3 in q returns True

Now you want to check falls in range then you can do like this

现在你想检查范围内的落差,那么你可以这样做

if -1<= 3 <= 4:
   print "Falls in the range"
else:
   print "not in range"

output:

输出:

Falls in the range

回答by Ravdeep

Use a if statement to find the order

使用 if 语句查找订单

if c > d:
    c, d = d, c 

r = range(c, d)

回答by TinnyT

Maybe this?

也许这个?

r = list(xrange(4,-1,-1))
3 in r

True

q = list(xrange(-1,4,1))
3 in q

True

回答by gboffi

The syntax of the rangebuiltin needs a little while to be grasped but when working with the interactive interpreter help(range)is always a helping hand...

range内置语法需要一点时间来掌握,但在使用交互式解释help(range)器时总是有帮助的......

Python 2.x

蟒蛇 2.x

>>> print range(4,-1)
>>> print range(-1,4)

Pyton 3.x

派顿 3.x

>>> print(list(range(4,-1)))
>>> print(list(range(-1,4)))

but there is a problem (perhaps not with your specific issue) even if you add a step argument to the invocation of range (example for Python 2.x)

但是即使您向 range 的调用添加了 step 参数(例如 Python 2.x),也存在问题(可能不是您的特定问题)

>>> print range(4, -1, -1)
>>> print range(-1,4)

as you can see, the two ranges are not the same: the startargument is in the list, the stopone is not.

如您所见,这两个范围并不相同:start参数在列表中,stop一个不在列表中。



You can think of it in these terms: "in a range, the stopargument means stop before".

你可以用这些术语来考虑它:“在 a 中rangestop参数意味着在之前停止”。



回答by Bill O

As answered by 101 and d-coder, your question to find a value betweentwo other values should be constructed using <or <=operators, depending on what you want. Assuming that you know that r is a simple collection, like a list or tuple or range, but you don't know it's ordering, you could write:

正如 101 和 d-coder 所回答的那样,您两个其他值之间找到一个值的问题应该使用<<=运算符来构造 ,具体取决于您想要什么。假设您知道 r 是一个简单的集合,如列表或元组或范围,但您不知道它的排序,您可以编写:

>>> r = (-1,4,2,3,0)
>>> min(r) < 3 < max(r)
True
>>> min(r) < 2.5 < max(r)
True

The inoperator on the other hand will test whether your value is one of the values in the list

in另一方面,运算符将测试您的值是否是列表中的值之一

>>> 3 in r
True
>>> 2.5 in r
False

Finally, your first example failed because you did not specify a negative step when trying to define 'r' as a decreasing sequence. Here's an example where I'll specify a step size of -2 as the 3rd argument of range():

最后,您的第一个示例失败了,因为您在尝试将 'r' 定义为递减序列时没有指定负步骤。这是一个示例,我将指定步长为 -2 作为 的第三个参数range()

>>> a = range(4,-1)
>>> print a
[]
>>> b = range(5,-1,-2)
>>> print b
[5, 3, 1]
>>> 3 in a
False
>>> 3 in b
True