Python:十进制数字的范围函数 [英] Python: range function for decimal numbers
问题描述
例如,对于浮点数
,Python中是否有任何range()函数
Is there any range() function in python for float numbers for example
a=0.6
if a in range(0,1):
a=3
我该如何实现呢?
推荐答案
如果我没看错,你想测试一个数字是否在另外两个之间数字,因此使用:
If I'm reading correctly, you want to test if a number is between two other numbers, so use:
a = 0.6
if 0 <= a < 1: # change to `<= 1` to be inclusive
a = 3
您无需生成范围并进行成员资格测试-除非您有 a
应该匹配的离散值集-内置的 range < Python 3.x中的/ code>可以对
int
s执行高效的查找,因为它可以优化成员资格测试。如果您在较大范围内有大量离散值,则最好还是在数学上进行计算。
You don't need to generate a range and do membership testing - unless you have a discrete set of values that your a
should match - the builtin range
in Python 3.x can do efficient lookups for int
s as it can optimise membership testing. If you have a large amount of discrete values in a large range, then you'd be better of doing it mathematically anyway.
这篇关于Python:十进制数字的范围函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!