[Tutor] List elements as indices to another list

2008-08-03 Thread Carlos Laviola
Hi,

I have a simple matrix (nested list) defined as such:

M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

I'm trying to come up with a different way of getting its, well,
reverse antidiagonal, since the actual antidiagonal of M goes from
M[0, N] to M[N, 0] according to
http://planetmath.org/encyclopedia/AntiDiagonalMatrix.html:

j = 0
for i in range(len(M)-1, -1, -1):
print M[i][j]
j += 1

This works fine, but I was looking for a different solution, just for
kicks, and came across something interesting. I can do:

 i, j = range(len(M)), range(len(M)-1, -1, -1)
 i
[0, 1, 2]
 j
[2, 1, 0]

Theoretically, I could then just iterate over range(len(M)) and grab
M[i[N]j[N]], but that's not legal. What would be the right way of doing
this?

Thanks in advance,
Carlos



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List elements as indices to another list

2008-08-03 Thread Alan Gauld


Carlos Laviola [EMAIL PROTECTED] wrote


i

[0, 1, 2]

j

[2, 1, 0]

Theoretically, I could then just iterate over range(len(M)) and grab
M[i[N]j[N]], but that's not legal. What would be the right way of 
doing

this?


M [ i[N] ]  [ j[N] ]

You just missed a couple of brackets...

HTH,

Alan G 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor