Multidimensional Array

0

I'm trying to create a multidimensional array. First I wanted to put all the first elements with value 2 so I did:

l=4
x=6
TSup=[ [ 2 for i in range(l) ] for j in range(x) ]
print TSup

And I got what I expected:

[[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]

Now I want to change the code so that the 1st value is the respective position of a Test list, that is, I wanted the 1st element to be 290, the 2nd 293, the 3rd 291 and the 4th 294, so I changed the following form:

Test=[290,293,291,294]
l=4
x=6
for a in range(len(Test)):

    TSup=[ [ Test[a] for i in range(l) ] for j in range(x) ]

print (TSup)

but does not give the expected, only gives the value for the last iteration, 4th position:

[[294, 294, 294, 294], [294, 294, 294, 294], [294, 294, 294, 294], [294, 294, 294, 294], [294, 294, 294, 294], [294, 294, 294, 294]] 

And I wanted to:

[[290, 293, 291, 294],[290, 293, 291, 294],[290, 293, 291, 294],[290, 293, 291, 294],[290, 293, 291, 294],[290, 293, 291, 294]]

If someone can help me, I really appreciate it!

    
asked by anonymous 20.03.2016 / 13:57

3 answers

1

The * operator is just for this:

n = 6
final = [[290, 293, 291, 294]]*n
    
01.06.2016 / 20:21
0

Do this:

for a in range(len(Test)):
    Tsup = [ [Test[i] for i in range(l)] for j in range(x) ]

or just like this:

Tsup = [ [Test[i] for i in range(l)] for j in range(x) ]
    
20.03.2016 / 14:14
0

We have to create 6 equal lists to test within a list 'mother':

test = [290,293,291,294]
final = [[i for i in test] for i in range(6)]
print(final) # [[290, 293, 291, 294], [290, 293, 291, 294], [290, 293, 291, 294], [290, 293, 291, 294], [290, 293, 291, 294], [290, 293, 291, 294]]
    
31.05.2016 / 22:31