Matrix (list-in-list), how to get a column?
Larry Bates
lbates at syscononline.com
Thu Oct 21 11:32:56 EDT 2004
Avid Andersson wrote:
> Good afternoon,
>
> I have some data that looks like this:
> data = [[1, 2], [3, 4], [5, 6]]
>
> I want to send columns 1 and 2 to a function
> as two variables, say "plot(col1,col2)".
>
> I can get a row by data[r], but how do I get a
> column? data[:][c] would have been my guess,
> but that returns the same list as data[r].
>
> I can solve this with two additional lists and
> a for loop, but that seems like an ugly hack.
> Five additional lines just seems clumsy. :-)
>
> Is there a more elegant solution than this?
>
> col1 = []
> col2 = []
> for i in range(len(data)):
> col1 += [a[i][0]]
> col2 += [a[i][1]]
>
>
> /Arvid Andersson
>
Try something like:
coldata=[[x[0] for x in data], [x[1] for x in data]]
plot(*coldata)
Larry Bates
Syscon, Inc.
More information about the Python-list
mailing list