How to use a decimal range() step value?

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

    Similar to R's seq function, this one returns a sequence in any order given the correct step value. The last value is equal to the stop value.

    def seq(start, stop, step=1):
        n = int(round((stop - start)/float(step)))
        if n > 1:
            return([start + step*i for i in range(n+1)])
        elif n == 1:
            return([start])
        else:
            return([])
    

    Results

    seq(1, 5, 0.5)
    

    [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]

    seq(10, 0, -1)
    

    [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

    seq(10, 0, -2)
    

    [10, 8, 6, 4, 2, 0]

    seq(1, 1)
    

    [ 1 ]

    0 讨论(0)
  • 2020-11-21 23:26

    start and stop are inclusive rather than one or the other (usually stop is excluded) and without imports, and using generators

    def rangef(start, stop, step, fround=5):
        """
        Yields sequence of numbers from start (inclusive) to stop (inclusive)
        by step (increment) with rounding set to n digits.
    
        :param start: start of sequence
        :param stop: end of sequence
        :param step: int or float increment (e.g. 1 or 0.001)
        :param fround: float rounding, n decimal places
        :return:
        """
        try:
            i = 0
            while stop >= start and step > 0:
                if i==0:
                    yield start
                elif start >= stop:
                    yield stop
                elif start < stop:
                    if start == 0:
                        yield 0
                    if start != 0:
                        yield start
                i += 1
                start += step
                start = round(start, fround)
            else:
                pass
        except TypeError as e:
            yield "type-error({})".format(e)
        else:
            pass
    
    
    # passing
    print(list(rangef(-100.0,10.0,1)))
    print(list(rangef(-100,0,0.5)))
    print(list(rangef(-1,1,0.2)))
    print(list(rangef(-1,1,0.1)))
    print(list(rangef(-1,1,0.05)))
    print(list(rangef(-1,1,0.02)))
    print(list(rangef(-1,1,0.01)))
    print(list(rangef(-1,1,0.005)))
    # failing: type-error:
    print(list(rangef("1","10","1")))
    print(list(rangef(1,10,"1")))
    

    Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]

    0 讨论(0)
  • 2020-11-21 23:27

    This is my solution to get ranges with float steps.
    Using this function it's not necessary to import numpy, nor install it.
    I'm pretty sure that it could be improved and optimized. Feel free to do it and post it here.

    from __future__ import division
    from math import log
    
    def xfrange(start, stop, step):
    
        old_start = start #backup this value
    
        digits = int(round(log(10000, 10)))+1 #get number of digits
        magnitude = 10**digits
        stop = int(magnitude * stop) #convert from 
        step = int(magnitude * step) #0.1 to 10 (e.g.)
    
        if start == 0:
            start = 10**(digits-1)
        else:
            start = 10**(digits)*start
    
        data = []   #create array
    
        #calc number of iterations
        end_loop = int((stop-start)//step)
        if old_start == 0:
            end_loop += 1
    
        acc = start
    
        for i in xrange(0, end_loop):
            data.append(acc/magnitude)
            acc += step
    
        return data
    
    print xfrange(1, 2.1, 0.1)
    print xfrange(0, 1.1, 0.1)
    print xfrange(-1, 0.1, 0.1)
    

    The output is:

    [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0]
    [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1]
    [-1.0, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0.0]
    
    0 讨论(0)
  • 2020-11-21 23:29

    And if you do this often, you might want to save the generated list r

    r=map(lambda x: x/10.0,range(0,10))
    for i in r:
        print i
    
    0 讨论(0)
  • 2020-11-21 23:29

    My answer is similar to others using map(), without need of NumPy, and without using lambda (though you could). To get a list of float values from 0.0 to t_max in steps of dt:

    def xdt(n):
        return dt*float(n)
    tlist  = map(xdt, range(int(t_max/dt)+1))
    
    0 讨论(0)
  • 2020-11-21 23:30

    My solution:

    def seq(start, stop, step=1, digit=0):
        x = float(start)
        v = []
        while x <= stop:
            v.append(round(x,digit))
            x += step
        return v
    
    0 讨论(0)
提交回复
热议问题