How to generically transform a list?

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Fri Aug 27 13:29:58 CEST 2004


Alex Martelli wrote:

> 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.

I'm not too keen on it myself, just pointing how it could be done.

-- 
"Codito ergo sum"
Roel Schroeven



More information about the Python-list mailing list