How to create a range of numbers with a given increment

后端 未结 6 893
故里飘歌
故里飘歌 2021-01-15 15:26

I want to know whether there is an equivalent statement in lists to do the following. In MATLAB I would do the following

fid = fopen(\'inc.txt\',\'w\')
init          


        
6条回答
  •  走了就别回头了
    2021-01-15 15:38

    In Python, range(start, stop + 1, step) can be used like Matlab's start:step:stop command. Unlike Matlab's functionality, however, range only works when start, step, and stop are all integers. If you want a parallel function that works with floating-point values, try the arange command from numpy:

    import numpy as np
    
    with open('numbers.txt', 'w') as handle:
        for n in np.arange(1, 5, 0.1):
            handle.write('{}\n'.format(n))
    

    Keep in mind that, unlike Matlab, range and np.arange both expect their arguments in the order start, stop, then step. Also keep in mind that, unlike the Matlab syntax, range and np.arange both stop as soon as the current value is greater than or equal to the stop value.

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html

提交回复
热议问题