Re: [Tutor] reverse diagonal

2012-12-02 Thread eryksun
On Sun, Dec 2, 2012 at 2:32 AM, Steven D'Aprano st...@pearwood.info wrote: ~i returns the value (-i - 1): Assuming certain implementation details about how integers are stored, namely that they are two-compliment rather than one-compliment or something more exotic. Yes, the result is

[Tutor] reverse diagonal

2012-12-01 Thread richard kappler
I'm working through Mark Lutz's Python, reviewing the section on lists. I understand the list comprehension so far, but ran into a snag with the matrix. I've created the matrix M as follows: M = [[1, 2, 3[, [4, 5, 6], [7, 8, 9]] then ran through the various comprehension examples, including:

Re: [Tutor] reverse diagonal

2012-12-01 Thread Ashfaq
revdiag = [M[i][i] for i in [2, 1, 0]] revdiag [9, 5, 1] The reverse diag entries (that you are seeking to get) are not correct. They should be M[0][2], M[1][1], M[2][0]. So the code could be -- revdiag = [] for i in [0, 1, 2]: j = 2 - i revdiag.append( M[i][j] ) I hope it helps. --

Re: [Tutor] reverse diagonal

2012-12-01 Thread Jonatán Guadamuz Espinoza
On Sat, Dec 1, 2012 at 9:40 AM, richard kappler richkapp...@gmail.com wrote: I'm working through Mark Lutz's Python, reviewing the section on lists. I understand the list comprehension so far, but ran into a snag with the matrix. I've created the matrix M as follows: M = [[1, 2, 3[, [4, 5,

Re: [Tutor] reverse diagonal

2012-12-01 Thread Dave Angel
On 12/01/2012 10:40 AM, richard kappler wrote: I'm working through Mark Lutz's Python, reviewing the section on lists. I understand the list comprehension so far, but ran into a snag with the matrix. I've created the matrix M as follows: M = [[1, 2, 3[, [4, 5, 6], [7, 8, 9]] There's an

Re: [Tutor] reverse diagonal

2012-12-01 Thread Brian van den Broek
On 1 December 2012 10:40, richard kappler richkapp...@gmail.com wrote: I'm working through Mark Lutz's Python, reviewing the section on lists. I understand the list comprehension so far, but ran into a snag with the matrix. I've created the matrix M as follows: M = [[1, 2, 3[, [4, 5, 6], [7,

Re: [Tutor] reverse diagonal

2012-12-01 Thread Dave Angel
On 12/01/2012 11:28 AM, Brian van den Broek wrote: On 1 December 2012 10:40, richard kappler richkapp...@gmail.com wrote: I'm working through Mark Lutz's Python, reviewing the section on lists. I understand the list comprehension so far, but ran into a snag with the matrix. I've created the

Re: [Tutor] reverse diagonal

2012-12-01 Thread Brian van den Broek
On 1 December 2012 20:12, Dave Angel d...@davea.name wrote: On 12/01/2012 11:28 AM, Brian van den Broek wrote: On 1 December 2012 10:40, richard kappler richkapp...@gmail.com wrote: I'm working through Mark Lutz's Python, reviewing the section on lists. I understand the list comprehension so

Re: [Tutor] reverse diagonal

2012-12-01 Thread eryksun
On Sat, Dec 1, 2012 at 11:31 AM, Dave Angel d...@davea.name wrote: revdiag = [M[i][len(M)-1-i] for i in range(len(M)) ] You might sometimes see this using the bitwise invert operator ~ (i.e. __invert__, operator.invert): M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [M[i][~i] for i in

Re: [Tutor] reverse diagonal

2012-12-01 Thread Dave Angel
On 12/01/2012 09:19 PM, eryksun wrote: On Sat, Dec 1, 2012 at 11:31 AM, Dave Angel d...@davea.name wrote: revdiag = [M[i][len(M)-1-i] for i in range(len(M)) ] You might sometimes see this using the bitwise invert operator ~ (i.e. __invert__, operator.invert): M = [[1, 2, 3], [4, 5,

Re: [Tutor] reverse diagonal

2012-12-01 Thread eryksun
On Sat, Dec 1, 2012 at 9:35 PM, Dave Angel d...@davea.name wrote: [M[i][~i] for i,dummy in enumerate(M) ] Since enumerate() iterates the rows, you could skip the first index: [row[~i] for i,row in enumerate(M)] [3, 5, 7] ___ Tutor maillist

Re: [Tutor] reverse diagonal

2012-12-01 Thread Dave Angel
On 12/01/2012 09:55 PM, eryksun wrote: On Sat, Dec 1, 2012 at 9:35 PM, Dave Angel d...@davea.name wrote: [M[i][~i] for i,dummy in enumerate(M) ] Since enumerate() iterates the rows, you could skip the first index: [row[~i] for i,row in enumerate(M)] [3, 5, 7] Great job.

Re: [Tutor] reverse diagonal

2012-12-01 Thread Steven D'Aprano
On Sat, Dec 01, 2012 at 09:19:57PM -0500, eryksun wrote: On Sat, Dec 1, 2012 at 11:31 AM, Dave Angel d...@davea.name wrote: revdiag = [M[i][len(M)-1-i] for i in range(len(M)) ] You might sometimes see this using the bitwise invert operator ~ (i.e. __invert__, operator.invert): M