[Tutor] Using lists as table-like structure

Fred Lionetti lordvader at gmail.com
Wed Nov 16 22:23:10 CET 2005


Hi Bernard,

You can do this with Numeric (http://numeric.scipy.org/)


import Numeric
aList = [ [1,1,1], [2,2,2,], [3,3,3] ]
bList = Numeric.array(aList, "i")
print bList[:,0]

displays->
[1 2 3]

you can convert the array back to a list if you want like this:
bList.tolist()

so...
result = Numeric.array(aList, "i")[:,0].tolist()

should do what you want in one line.


Fred

> aList = [ [1,1,1], [2,2,2,], [3,3,3] ]
> aList[:][0] = 10
>
>
> If I print aList[:], I get the list with the nested sublists.
>
> >>> aList[:]
> [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
>
>
> But as soon as I introduce the [0], in an attempt to access the first
> element of each sublist, I get the first sublist in its entirety:
>
> >>> aList[:][0]
> [1, 1, 1]
>
>
> I would have hoped to get something like
> [1, 2, 3]
>
>
> Is this possible at all?
>
> Thanks
> Bernard
>


More information about the Tutor mailing list