Python: range function for decimal numbers(Python:十进制数的范围函数)
问题描述
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
应该匹配 - Python 中的内置 range
3.x 可以对 int
进行有效的查找,因为它可以优化成员资格测试.如果你在一个很大的范围内有大量的离散值,那么你最好还是用数学方法来做.
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:十进制数的范围函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python:十进制数的范围函数


基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01