[Tutor] Nesting lists?
Magnus Lyckå
magnus@thinkware.se
Sat Apr 26 05:11:02 2003
At Fri, 25 Apr 2003 19:13:46 +0100, Alan Gauld wrote:
> > >array in python but a list. What I'd like to do is something to
>the
> > >effect of:
> > >
> > >data = [data1[], data2[], data3[]]
> > >
>data1 = []
>data2 = []
>data3 = []
>data = [data1, data2, data3]
>
>Or even:
>
>data = [ [], [], [] ]
>
>data[0].append(1) #-> [ [1], [], [] ]
If you wan't to use this array of array for numeric values,
you might want to have a look at "Numeric", see
http://www.pfdubois.com/numpy/
E.g.
>>> import Numeric
>>> a = Numeric.array([[1,2,3],[4,5,6],[7,8,9]])
>>> b = Numeric.array([[3,2,1],[6,5,4],[9,8,7]])
>>> print a
[[1 2 3]
[4 5 6]
[7 8 9]]
>>> print b
[[3 2 1]
[6 5 4]
[9 8 7]]
>>> c = a * b
>>> print c
[[ 3 4 3]
[24 25 24]
[63 64 63]]
>>> # That was just an elementwise multiplication.
>>> d = Numeric.matrixmultiply(a, b)
>>> print d
[[ 42 36 30]
[ 96 81 66]
[150 126 102]]
>>> # Aha, here we have a proper matrix multiplication.
>>> Numeric.transpose(d)
array([[ 42, 96, 150],
[ 36, 81, 126],
[ 30, 66, 102]])
# etc...
--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program