PDA

View Full Version : Python Fill A 3d List



tdrusk
October 25th, 2009, 10:29 PM
I am working on a program that uses the users input and creates a 3d list for the amount of input. For example:

a = ['u','b','u','n','t','u']

I want it to create an list using the length of array a like


b =[
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]],
[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]],
]


How can I do this using lists and the len(a)?

Can+~
October 25th, 2009, 10:36 PM
a = ['u','b','u','n','t','u']

First of all, why would you split a string into a list? A string already is iterable, and already has a __len__ method:


a = "ubuntu"
n = len(a) # returns 6

Second, you can use multiplication:


>>> [0]*5
[0, 0, 0, 0, 0]


Therefore (I manually added some spacing, to make it easier to see):


>>> [[[0]*3]*n]*n
[[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]


Why would you need a 3d-matrix? (Also, this isn't a matrix, it behaves like one, but in truth, is a list in a list in a list. If you want real matrices, use scipy)

tdrusk
October 25th, 2009, 10:42 PM
a = ['u','b','u','n','t','u']First of all, why would you split a string into a list? A string already is iterable, and already has a __len__ method:


a = "ubuntu"
n = len(a) # returns 6Second, you can use multiplication:


>>> [0]*5
[0, 0, 0, 0, 0]
Therefore (I manually added some spacing, to make it easier to see):


>>> [[[0]*3]*n]*n
[[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]
Why would you need a 3d-matrix? (Also, this isn't a matrix, it behaves like one, but in truth, is a list in a list in a list. If you want real matrices, use scipy)

My code is already written using a as an array and 3d list in mind. Right now the lengths of a and b are static, but I would like to make my program useful for other lengths.

diesch
October 25th, 2009, 11:02 PM
>>> [[[0]*3]*n]*n
[[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]


Note that all this [0,0,0] are the same object - which is most likely not what one wants here:


>>> a=[[[0]*3]*n]*n
>>> a[0][0][0]=5
>>> a
[[[5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0]], [[5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0]], [[5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0]], [[5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0]], [[5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0]], [[5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0], [5, 0, 0]]]

diesch
October 25th, 2009, 11:08 PM
My code is already written using a as an array and 3d list in mind. Right now the lengths of a and b are static, but I would like to make my program useful for other lengths.

Use numpy, see http://www.scipy.org/Cookbook/BuildingArrays

Can+~
October 25th, 2009, 11:10 PM
Diesch: You're right. I used the x*multiplier method when using numeral types, and I didn't think that it would copy references.

Here's a generator:


makematrix = lambda n: [[[0,0,0] for a in xrange(0, n)] for b in xrange(0, n)]
makematrix(6)

tdrusk
October 25th, 2009, 11:38 PM
Diesch: You're right. I used the x*multiplier method when using numeral types, and I didn't think that it would copy references.

Here's a generator:


makematrix = lambda n: [[[0,0,0] for a in xrange(0, n)] for b in xrange(0, n)]
makematrix(6)
This worked.

Took me a minute to realize I had to set
b = makematrix(6)

Thanks!