0

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']
5
  • Do you want a list of numbers like that list or do you want exactly that list of numbers? Do you know if can be generated by some algorithm or is it an arbitrary random list? Commented Dec 6, 2020 at 14:31
  • @JeffUK Jeff, what I want is a list with exact numbers. It would be [4, 7, 8, 11, 12......] Commented Dec 6, 2020 at 14:35
  • and how was that list generated? Commented Dec 6, 2020 at 14:43
  • @JeffUK My code could print them like the above, but I can't put them into a list. Commented Dec 6, 2020 at 14:46
  • So you're saying your algorithm works, you just need to know how to append these numbers to a list instead of printing them sequentially? Commented Dec 6, 2020 at 14:48

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

It works to find what I want. Thank for your help!
1

Do not think you need a loop for this, let alone two:

import numpy as np
dif = np.ones(100, dtype = np.int32)
dif[np.cumsum(np.arange(14))] = 3
(1+np.cumsum(dif)).tolist()

output

[4, 7, 8, 11, 12, 13, 16, 17, 18, 19, 22, 23, 24, 25, 26, 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 41, 42, 43, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 61, 62, 63, 64, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 121, 122, 123, 124, 125, 126, 127, 128, 129]

Comments

0
ind_list = []
start_ind = 4
for x in range(0, 6):
        ind_list.append(start_ind)
        for y in range(1, x+1):
            ind_list.append(start_ind + y)
        start_ind = ind_list[len(ind_list)-1]+3
print(ind_list)

You could probably use this. the print function works fine, the list I assume works fairly well for the numbers provided. It appends the new number at the beginning of the loop, with a cotinually longer loop each time for x. I'm assuming the number sequence is 4, 4+3, 4+3+1, 4+3+1+3, 4+3+1+3+1, 4+3+1+3+1+1, 4+3+1+3+1+1+3, ....

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.