Matrix (list-in-list), how to get a column?

A

Arvid Andersson

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[0]]
col2 += [a[1]]


/Arvid Andersson
 
L

Larry Bates

Avid said:
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[0]]
col2 += [a[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.
 
A

Alex Martelli

Arvid Andersson said:
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].

Right: data[:] makes a shallow copy of data, then you index into that --
no use.

col_c = [row[c] for row in data]

is probably the simplest, best and most idiomatic way to extract column
'c' from this kind of list-of-lists.
Is there a more elegant solution than this?

col1 = []
col2 = []
for i in range(len(data)):
col1 += [a[0]]
col2 += [a[1]]


Several -- this one has not a few small imperfections. For example,
there is generally no need to iterate on indices:

for row in data:
col1 += [row[0]]

&c, would already be a small enhancement. Moreover,
'col1 += [something]' is just a complicated way to express
col1.append(something), so you could further move to:

for row in data:
col1.append(row[0])

&c. And finally, you can recognize the pattern that:

anylist = []
for item in somewhere:
anylist.append(<expression using item>)

is exactly what's meant by a list comprehension:

anylist = [<expression using item> for item in somewhere]

thus getting to the above-suggested

col1 = [row[0] for row in data]

and the like. Since a list comprehension is an expression, you don't
have to give it a name if you don't want to; just pass it as the
argument in the function call. Moreover, if you do this thing often,
much legibility at the cost of a tiny overhead could be had by having

def col(data, colindex):
return [row[colindex] for row in data]

and using col(data, 0) and col(data, 1) as arguments to the function
you're calling.


Alex
 
F

F. Petitjean

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)".
From a interpreter session :
data = [[1, 2], [3, 4], [5, 6]]
col1 = [ pt[0] for pt in data ]
col1
[1, 3, 5]
array([[1, 2],
[3, 4],
[5, 6]])array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
pts = N.array(data, N.Float)
pts[:,0]
array([ 1., 3., 5.])

Hope that helps. Don't hesitate to try directly at the interpreter
prompt.
 
R

Russell Blau

F. Petitjean said:
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)". ....
import Numeric as N
....

Hope that helps. Don't hesitate to try directly at the interpreter
prompt.

Ok, let's see what happens:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named Numeric

I think you left out a step in your instructions.... ;-)
 
P

Peter Otten

Arvid said:
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)".

In the general case list comprehensions are the way to go, but for the
problem specified above you can use a neat zip() trick, assuming that
plot() also accepts tuples instead of lists:
.... print "col1", col1
.... print "col2", col2
....
data = [[1, 2], [3, 4], [5, 6]]
plot(*zip(*data))
col1 (1, 3, 5)
col2 (2, 4, 6)

The star prefix feeds the items in the following list as arguments to the
function, so zip(*[[1, 2], [3, 4]]) is the same as zip([1, 2], [3, 4]),
which in turn gives you [(1, 3), (2, 4)] as the result.
The same technique can then be repeated with plot().

Peter
 
F

F. Petitjean

F. Petitjean said:
I have some data that looks like this:
data = [[1, 2], [3, 4], [5, 6]]
...
import Numeric as N
...

Hope that helps. Don't hesitate to try directly at the interpreter
prompt.

Ok, let's see what happens:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named Numeric

I think you left out a step in your instructions.... ;-)
Numeric is the name of the "Numeric python" package aka Numpy.
I think that you can find it on sourceforge.net, alongside a new version
called Numarray. So, The numeric arrays are interesting as they store
homogeneous items (typically integers or floats) and can have multiple
dimensions. Slicing (pts[:,1] for instance) is very convenient.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top