Select column from a list
contact at xavierho.com
Fri Aug 28 09:55:50 CEST 2009
On Fri, Aug 28, 2009 at 5:45 PM, hoffik <beorn at seznam.cz> wrote:
> I'm quite new in Python and I have one question. I have a 2D matrix of
> values stored in list (3 columns, many rows). I wonder if I can select one
> column without having to go through the list with 'for' command.
As far as I know though, you will have to loop through this if you built
your matrix with nested lists.
li = [[1,2],[3,4]]
result = 
for row in li:
# result == [1, 3]
> For example I have list called 'values'.
> When I write 'values' or 'values[:]' I'll get the first row.
> But when I write 'values[:]' I won't get the first column, but the first
> row again! I can't see why.
Let's say values = [[1,2][3,4]].
values[:] returns the entire values list, from beginning to end. So you end
up what you started with.
And, of course, values[:] is the same as values.
>>> li[:][:][:] is li
It's... a very interesting behaviour, to say the least.
You might be able to get a proper matrix behaviour using NumPy, (which I
never used, so I'm just throwing it out there as a guess) or you might have
to write your own iterator for later reuse.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list