Home > Software engineering >  How can I make this python function generate such a list [[1], [1, 2]...[1, 2, 3, 4, 5, 6, 7, 8, 9,
How can I make this python function generate such a list [[1], [1, 2]...[1, 2, 3, 4, 5, 6, 7, 8, 9,

Time:02-02

all = []
def generate(i, current):
    if i < 11:
        current.append(i)
        all.append(current)
        i = 1
        generate(i, current)
generate(1, [])
print(all)

I want this function to generate

[[1], [1, 2]...[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]] 

instead of

[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], 

but don't know how to fix it.

Do you know the solution?

CodePudding user response:

Here's my go:

def listGen(start, stop):
    res = []
    for i in range(start, stop 1):
        res.append([x for x in range(start, i 1)])
    return res

You could also simplify this to:

def listGen(start, stop):
    return [[x for x in range(start, i 1)] for i in range(start, stop 1)]
Input: print(listGen(1, 10))
Output: [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

CodePudding user response:

def generate_array():
    result = []
    for i in range(1, 11):
        current_array = []
        for j in range(1, i   1):
            current_array.append(j)
        result.append(current_array)
    return result

print(generate_array())

The code uses two nested for loops, where the outer loop iterates over range(1, 11) and the inner loop iterates over range(1, i 1). The values of i and j are used to generate the sublists and append them to the result list, which is returned at the end of the function.

CodePudding user response:

The core issue you have is that when you do:

all.append(current)

current is the exact same list all over the place so when you append to it in the prior line you effectively append to it everywhere. To fix that and the lightest change to your code you would append to copy of it.:

all = []
def generate(i, current):
    if i < 11:
        current.append(i)
        all.append(current.copy())  ## <--- append a copy
        i = 1
        generate(i, current)
generate(1, [])
print(all)

alternatively you could pass a copy like:

all = []
def generate(i, current):
    if i < 11:
        current.append(i)
        all.append(current)
        i = 1
        generate(i, current.copy()) ## <--- pass a copy
generate(1, [])
print(all)

In either case, the important part is that we get a distinct current to work with.

Note that the use of all as a variable clobbers the function all() and you might not want to do that. As I'm sure lots of others will point out, there are many ways to skin this cat.

  • Related