Skip to content Skip to sidebar Skip to footer

How To Create A Range Of Numbers With A Given Increment

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 =1;inc = 5; final=51; a = i

Solution 1:

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

withopen('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

Solution 2:

You can easily create a function for this. The first three arguments of the function will be the range parameters as integers and the last, fourth argument will be the filename, as a string:

defrange_to_file(init, final, inc, fname):
    withopen(fname, 'w') as f:
        f.write('\n'.join(str(i) for i inrange(init, final, inc)))

Now you have to call it, with your custom values:

range_to_file(1, 51, 5, 'inc.txt')

So your output will be (in the fname file):

1
6
11
16
21
26
31
36
41
46

NOTE: in Python 2.x arange()returns a list, in Python 3.x arange()returns an immutable sequence iterator, and if you want to get a list you have to writelist(range())

Solution 3:

test.py contains :

#!/bin/env python                                                                                                                                                                                  

f = open("test.txt","wb")                                                                                                                                                                           
for i in range(1,50,5):                                                                                                                                                                             
    f.write("%d\n"%i)

f.close()

You can execute

python test.py

file test.txt would look like this :

1
6
11
16
21
26
31
36
41
46

Solution 4:

I think your looking for something like this:

nums = range(10) #or any list, i.e. [0, 1, 2, 3...]number_string = ''.join([str(x) for x in nums])

The [str(x) for x in nums] syntax is called a list comprehension. It allows you to build a list on the fly. '\n'.join(list) serves to take a list of strings and concatenate them together. str(x) is a type cast: it converts an integer to a string.

Alternatively, with a simple for loop:

number_string = ''for num in nums:
    number_string += str(num)

The key is that you cast the value to a string before concatenation.

Solution 5:

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 = 1final = 51
inc = 5
with open('inc.txt','w') as myfile:
    for nn in xrange(init, final+inc, inc):
        myfile.write('%d\n'%nn)

Post a Comment for "How To Create A Range Of Numbers With A Given Increment"