1

I want to:
Create a vector list from 0 to 4, i.e. [0, 1, 2, 3, 4] and from that
Create a matrix containing a "tiered list" from 0 to 4, 3 times over, once for each dimension. The matrix has 4^3 = 64 rows, so for example

T = [0 0 0    
       0 0 1  
       0 0 2  
       0 0 3  
       0 0 4  
       0 1 0  
       0 1 1  
       0 1 2  
       0 1 3  
       0 1 4  
       0 2 0  
       ...  
       1 0 0  
       ...  
       1 1 0  
       ....  
       4 4 4]    

This is what I have so far:

n=5;
ind=list(range(0,n))
print(ind)

I am just getting started with Python so any help would be greatly appreciated!

3 Answers 3

4

The python itertools module product() function can do this:

for code in itertools.product( range(5), repeat=3 ):
    print(code)

Giving the result:

(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 0, 3)
...
(4, 4, 2)
(4, 4, 3)
(4, 4, 4)

So to make this into a matrix:

import itertools
matrix = []
for code in itertools.product( range(5), repeat=3 ):
     matrix.append( list(code) )
Sign up to request clarification or add additional context in comments.

2 Comments

@Tomothy32 - I had to go lookup the syntax, you beat me to it. I don't like the "pythonic" way, it's unreadable, especially for a beginner.
Also, matrix = [list(code) for code in itertools.product(range(5), repeat=3)] is easier and more Pythonic.
0
 list_ = []
 for a in range(5):
     for b in range(5):
        for c in range(5):
              list_ += [a ,b ,c ]

 print(list_)

2 Comments

This is incorrect. First, it makes a list that is one dimensional instead of having many rows of 3. Second, what would be the rows are backwards compared to what is asked for.
The OP mentions matrix in their question but offers up a python list T = [0 0 0 etc with no commas just newlines and spaces
0

Note, you really want the matrix to have 5^3 = 125 rows. The basic answer is to just iterate in nested for loops:

T = []
for a in range(5):
    for b in range(5):
        for c in range(5):
            T.append([a, b, c])

There are other, probably faster, ways of doing this, but for sheer get 'er done velocity, it's hard to beat this.

6 Comments

Wouldn't it be T.append([a, b, c])?
That would make the order backwards compared to what OP is asking for.
Are you sure? Running it on my machine: [[0, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0], [4, 0, 0], [0, 1, 0], [1, 1, 0], [2, 1, 0], [3, 1, 0], [4, 1, 0], [0, 2, 0], [1, 2, 0], [2, 2, 0], [3, 2, 0], [4, 2, 0], [0, 3, 0], [1, 3, 0], [2, 3, 0], [3, 3, 0], [4, 3, 0], [0, 4, 0], [1, 4, 0], [2, 4, 0], [3, 4, 0], [4, 4, 0], [0, 0, 1], [1, 0, 1], [2, 0, 1], [3, 0, 1], [4, 0, 1], [0, 1, 1], [1, 1, 1], [2, 1, 1], [3, 1, 1], [4, 1, 1], [0, 2, 1], [1, 2, 1], [2, 2, 1], [3, 2, 1], [4, 2, 1], [0, 3, 1], [1, 3, 1], [2, 3, 1], [3, 3, 1], [4, 3, 1], [0, 4, 1], [1, 4, 1], [2, 4, 1], [3, 4, 1] ...
Compare your result carefully with what OP asked for. It was [[0,0,0], [0,0,1], [0,0,2], ... ]
Exactly, running your solution outputs the wrong result.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.