How to use a decimal range() step value?

前端 未结 30 2168
醉话见心
醉话见心 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:21

    The trick to avoid round-off problem is to use a separate number to move through the range, that starts and half the step ahead of start.

    # floating point range
    def frange(a, b, stp=1.0):
      i = a+stp/2.0
      while i

    Alternatively, numpy.arange can be used.

提交回复
热议问题