How to generically transform a list?

Alex Martelli aleaxit at yahoo.com
Fri Aug 27 06:52:02 EDT 2004


Roel Schroeven <rschroev_nospam_ml at fastmail.fm> wrote:

> Roel Schroeven wrote:
> 
> >> Just out of pure curiosity: Is there a langue that allows vertical and
> >> horizontal slicing and dicing with the same built-in pattern?
> > 
> > 
> > You can do it (sorta) in Python: use zip to turn the columns into rows
> > and vice-versa, apply the slicing, than zip back:
> > 
> >  >>> l = [['a', 1, 11, 'aa'], ['b', 2, 22, 'bb'], ['c', 3, 33, 'cc']]
> >  >>> zip(*(zip(*l)[2:0:-1]))
> > [(11, 1), (22, 2), (33, 3)]
> 
> You end up with a list of tuples instead of a list of lists though.

...so you map(list, zip(*(zip(*l)[2:0:-1]))) if you're really keen on
this approach.  Personally, I find it a disaster.


Alex
 



More information about the Python-list mailing list