Skip to content
Advertisement

How to generate a list of numbers in python

guys. I am now working on a python algorithm and I am new to python. I’d like to generate a list of numbers like 4, 7, 8, 11, 12, 13, 16, 17, 18, 19, 22, 23, 24, 25... with 2 for loops.

I’ve done some work to find some numbers and I am close to the result I want, which is generate a list contains this numbers

My code is here:

for x in range(0, 6, 1):
        start_ind = int(((x+3) * (x+2)) / 2 + 1)
        print("start index is ", [start_ind], x)
        start_node = node[start_ind]
        for y in range(0, x):
            ind = start_ind + y + 1
            ind_list = node[ind]
            index = [ind_list]
            print(index)

Node is a list:

node = ['n%d' % i for i in range(0, 36, 1)]

What I received from this code is:

start index is  [7] 1
['n8']
start index is  [11] 2
['n12']
['n13']
start index is  [16] 3
['n17']
['n18']
['n19']
start index is  [22] 4
['n23']
['n24']
['n25']
['n26']
start index is  [29] 5
['n30']
['n31']
['n32']
['n33']
['n34']

Advertisement

Answer

This seems to give the same list: and I think it’s much clearer what’s happening!

val=4
result=[]
for i in range(1,7):
    for j in range(val,val+i):
       val = val+1
       result.append(j)
    val = j+3

print(result)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement