map() return of flat tuple list

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Jun 23 05:11:02 EDT 2006


Mirco:
>He, this looks more like Haskell than like Python (for me, it looks awful ;-)

Maybe this is more readable:

ar = [[3,3,3,3],
      [3,3,3,1],
      [3,3,4,3]]

print sorted( [(r,c) for r,row in enumerate(ar) for c in
xrange(len(row))],
              key=lambda (r,c): ar[r][c]
            )[0]

I don't know if operator.itemgetter can be used here, I think it's too
much complex.

With python 2.5 you can probably simplify it a little (and speed it up)
with something like:

print min( [ (r,c) for r,row in enumerate(ar) for c in xrange(len(row))
],
           key=lambda (r,c): arr[r][c]
         )

Bye,
bearophile




More information about the Python-list mailing list