How to create a range of numbers with a given increment

后端 未结 6 891
故里飘歌
故里飘歌 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:39

    I think that the original poster wanted 51 to show up in the list, as well.

    The Python syntax for this is a little awkward, because you need to provide for range (or xrange or arange) an upper-limit argument that is one increment beyond your actual desired upper limit. An easy solution is the following:

    init = 1
    final = 51
    inc = 5
    with open('inc.txt','w') as myfile:
        for nn in xrange(init, final+inc, inc):
            myfile.write('%d\n'%nn)
    

提交回复
热议问题