Hi Guys,
I'm currently making the transition from matlab to python and am having some difficulties with nesting for loops.
Below is a rough example of what I'm trying to do. However when this executes abc is only returned as [] and not a 1D array of each iteration.

Any help would be greatly appreciated.

    n=5
    abc=[]

    for j in range(n-1,0):
        for i in range(0,j):
            a=2**i * 3**(j)
            abc.append(a)
    print abc

Recommended Answers

All 3 Replies

what is this supposed to mean, n is not <= 0?

range(n-1,0)

If I assume you mean range(n-1) (loop 0, 1, 2, 3) then the same result I would make with this with list comprehension

n = 5
print [2**i * 3**j for j in range(n-1) for i in range(j)]

Thanks for the reply.

range(n-1,0)

is meant to mean a decreasing sequence from n-1 to 0. I think see my error, it's matlab terminolgy.
It should instead read

range(n-1,0,-1)
range(n-1,0,-1)

You said until 0, but this is down to 1 loop, end is not inclusive (n-1 numbers starting from n-1).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.