On Fri, Aug 28, 2009 at 5:45 PM, hoffik <span dir="ltr"><<a href="mailto:beorn@seznam.cz">beorn@seznam.cz</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br>
<br>
I'm quite new in Python and I have one question. I have a 2D matrix of<br>
values stored in list (3 columns, many rows). I wonder if I can select one<br>
column without having to go through the list with 'for' command.</blockquote><div><br>As far as I know though, you will have to loop through this if you built your matrix with nested lists.<br><br>i.e.:<br><br>li = [[1,2],[3,4]]<br>
result = []<br>for row in li:<br> result.append(row[0])<br># result == [1, 3]<br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
For example I have list called 'values'.<br>
When I write 'values[0]' or 'values[0][:]' I'll get the first row.<br>
But when I write 'values[:][0]' I won't get the first column, but the first<br>
row again! I can't see why.</blockquote><div><br>Let's say values = [[1,2][3,4]].<br>values[:] returns the entire values list, from beginning to end. So you end up what you started with.<br>And, of course, values[:][0] is the same as values[0].<br>
<br>In fact:<br><br>>>> li[:][:][:][0] is li[0]<br>True<br><br>It's... a very interesting behaviour, to say the least. <br> <br>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.<br>
<br>Cheers,<br>Xav<br></div></div>