以十进制数为步骤的python中的for循环 [英] for loop in python with decimal number as step
本文介绍了以十进制数为步骤的python中的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想制作一个如下所示的 for 循环:
I want to make a for loop that looks like this:
for x in range(0, 1, 0.1):
print(x)
显然它会抛出这个错误:
obviously it throws this error:
回溯(最近一次调用最后一次):文件",第 1 行,在对于范围内的 i (0, 1, 0.1):类型错误:'float' 对象不能解释为整数
Traceback (most recent call last): File "", line 1, in for i in range(0, 1, 0.1): TypeError: 'float' object cannot be interpreted as an integer
那么在python 3.6中有没有办法用浮点数创建for循环?
So it there a way in python 3.6 to make a for loop with floating point?
推荐答案
使用 numpy
的 arange 而不是 range:
use numpy
's arange instead of range:
import numpy as np
for x in np.arange(0, 1, 0.1):
print(x)
这篇关于以十进制数为步骤的python中的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文