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:
JavaScript
x
10
10
1
for x in range(0, 6, 1):
2
start_ind = int(((x+3) * (x+2)) / 2 + 1)
3
print("start index is ", [start_ind], x)
4
start_node = node[start_ind]
5
for y in range(0, x):
6
ind = start_ind + y + 1
7
ind_list = node[ind]
8
index = [ind_list]
9
print(index)
10
Node is a list:
JavaScript
1
2
1
node = ['n%d' % i for i in range(0, 36, 1)]
2
What I received from this code is:
JavaScript
1
21
21
1
start index is [7] 1
2
['n8']
3
start index is [11] 2
4
['n12']
5
['n13']
6
start index is [16] 3
7
['n17']
8
['n18']
9
['n19']
10
start index is [22] 4
11
['n23']
12
['n24']
13
['n25']
14
['n26']
15
start index is [29] 5
16
['n30']
17
['n31']
18
['n32']
19
['n33']
20
['n34']
21
Advertisement
Answer
This seems to give the same list: and I think it’s much clearer what’s happening!
JavaScript
1
10
10
1
val=4
2
result=[]
3
for i in range(1,7):
4
for j in range(val,val+i):
5
val = val+1
6
result.append(j)
7
val = j+3
8
9
print(result)
10