0

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
2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by pyTony
0

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)]

Edited by pyTony

0

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)

Edited by Mitch275

0
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).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.