How to use a decimal range() step value?

前端 未结 30 2139
醉话见心
醉话见心 2020-11-21 22:34

Is there a way to step between 0 and 1 by 0.1?

I thought I could do it like the following, but it failed:

for i in range(0, 1, 0.1):
    print i
         


        
30条回答
  •  抹茶落季
    2020-11-21 23:11

    more_itertools is a third-party library that implements a numeric_range tool:

    import more_itertools as mit
    
    
    for x in mit.numeric_range(0, 1, 0.1):
        print("{:.1f}".format(x))
    

    Output

    0.0
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7
    0.8
    0.9
    

    This tool also works for Decimal and Fraction.

提交回复
热议问题