步长为 float 类型的范围
文档基本上说 range
的行为必须完全相同此实现(对于正步骤
):
def range(start, stop, step):
x = start
while True:
if x >= stop: return
yield x
x += step
它还表明其参数必须是整数。这是为什么?如果步骤是浮点数,这个定义不是也完全有效吗?
就我而言,我尤其如此。需要一个 range
函数,该函数接受 float 类型作为其 step
参数。 Python 有吗?还是我需要自己实现?
更具体地说:我如何以一种很好的方式将这个 C 代码直接转换为 Python(即不仅仅是通过 while 循环手动执行):
for(float x = 0; x < 10; x += 0.5f) { /* ... */ }
The documentation basically says that range
must behave exactly as this implementation (for positive step
):
def range(start, stop, step):
x = start
while True:
if x >= stop: return
yield x
x += step
It also says that its arguments must be integers. Why is that? Isn't that definition also perfectly valid if step is a float?
In my case, I am esp. needing a range
function which accepts a float type as its step
argument. Is there any in Python or do I need to implement my own?
More specific: How would I translate this C code directly to Python in a nice way (i.e. not just doing it via a while
-loop manually):
for(float x = 0; x < 10; x += 0.5f) { /* ... */ }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用
numpy.arange
。编辑:文档更喜欢
numpy.linspace< /代码>
。 感谢@Droogans 的关注 =)
You could use
numpy.arange
.EDIT: The docs prefer
numpy.linspace
. Thanks @Droogans for noticing =)一种解释可能是浮点舍入问题。例如,如果您可以调用,
您可能期望输出为
,但实际上
由于舍入问题,您会得到类似的结果。由于范围经常用于生成某种类型的索引,因此它只是整数。
不过,如果您想要一个浮动范围生成器,您可以自己创建。
One explanation might be floating point rounding issues. For example, if you could call
you might expect an output of
but you in fact get something like
due to rounding issues. And since range is often used to generate indices of some sort, it's integers only.
Still, if you want a range generator for floats, you can just roll your own.
为了能够在范围表达式中使用十进制数字,一种很酷的方法如下:
[x * 0.1 对于范围 (0, 10) 内的 x]
In order to be able to use decimal numbers in a range expression a cool way for doing it is the following:
[x * 0.1 for x in range(0, 10)]
浮点的问题是,由于不准确,您可能无法获得与预期相同数量的项目。如果您正在使用多项式,其中项目的确切数量非常重要,这可能是一个真正的问题。
你真正想要的是算术级数;下面的代码对于
int
、float
和complex
...以及字符串和列表非常满意...请注意,此代码代表与任何保持运行总计的替代方案相比,您的最后一个值更有可能处于预期值的公牛咆哮范围内。
更正:这里有一个竞争性运行总计小工具:
The problem with floating point is that you may not get the same number of items as you expected, due to inaccuracy. This can be a real problem if you are playing with polynomials where the exact number of items is quite important.
What you really want is an arithmetic progression; the following code will work quite happily for
int
,float
andcomplex
... and strings, and lists ...Note that this code stands a better chance of your last value being within a bull's roar of the expected value than any alternative which maintains a running total.
Correction: here's a competetive running-total gadget:
将浮点数相加时,通常会出现一些错误。
range(0.0, 2.2, 1.1)
会返回[0.0, 1.1]
或[0.0, 1.1, 2.199999999]
吗?如果没有严格的分析,就无法确定。如果您确实需要的话,您发布的代码是一个不错的解决方法。只是要注意可能的缺点。
When you add floating point numbers together, there's often a little bit of error. Would a
range(0.0, 2.2, 1.1)
return[0.0, 1.1]
or[0.0, 1.1, 2.199999999]
? There's no way to be certain without rigorous analysis.The code you posted is an OK work-around if you really need this. Just be aware of the possible shortcomings.
这是一个可能足够好的特殊情况:
在您的情况下,这将是:
所以:
Here is a special case that might be good enough:
In your case this would be:
and so:
这就是我会使用的:
而不是:
希望它有帮助。
This is what I would use:
rather than:
hope it helps.
可能是因为你不能拥有可迭代的一部分。此外,
float
并不精确。Probably because you can't have part of an iterable. Also,
floats
are imprecise.