Velocity Reviews > Re: How to generically transform a list?

# Re: How to generically transform a list?

Marco Aschwanden
Guest
Posts: n/a

 08-27-2004

Thanks to all the hints which seem to prove that there is one and only one
sensible approach - though some (like me) needed a few more lines.

It is just funny how easy horizontal slicing is made (list[:]) but how
"difficult" vertical slicing is. It is a common task and one does not
realize how often one does need vertical slicing. eg.: getting the keys of
dictionary is a vertical slicing, or turning a list into a dict involves
vertical slicing...

Just out of pure curiosity: Is there a langue that allows vertical and
horizontal slicing and dicing with the same built-in pattern?

Regards,
Marco (Eagerly awaiting the cookbook's second edition)

Roel Schroeven
Guest
Posts: n/a

 08-27-2004
Marco Aschwanden wrote:
>
> Thanks to all the hints which seem to prove that there is one and only
> one sensible approach - though some (like me) needed a few more lines.
>
> It is just funny how easy horizontal slicing is made (list[:]) but how
> "difficult" vertical slicing is. It is a common task and one does not
> realize how often one does need vertical slicing. eg.: getting the keys
> of dictionary is a vertical slicing, or turning a list into a dict
> involves vertical slicing...
>
> 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)]

Step-by-step to see what happens:

>>> zip(*l)

[('a', 'b', 'c'), (1, 2, 3), (11, 22, 33), ('aa', 'bb', 'cc')]
>>> zip(*l)[2:0:-1]

[(11, 22, 33), (1, 2, 3)]
>>> zip(*(zip(*l)[2:0:-1]))

[(11, 1), (22, 2), (33, 3)]

--
"Codito ergo sum"
Roel Schroeven

Roel Schroeven
Guest
Posts: n/a

 08-27-2004
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.

--
"Codito ergo sum"
Roel Schroeven

Alex Martelli
Guest
Posts: n/a

 08-27-2004
Roel Schroeven <(E-Mail Removed)> 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

Roel Schroeven
Guest
Posts: n/a

 08-27-2004
Alex Martelli wrote:

> Roel Schroeven <(E-Mail Removed)> 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

Scott David Daniels
Guest
Posts: n/a

 08-28-2004
Marco Aschwanden wrote:

>
> Thanks to all the hints which seem to prove that there is one and only
> one sensible approach - though some (like me) needed a few more lines.
>
> It is just funny how easy horizontal slicing is made (list[:]) but how
> "difficult" vertical slicing is. It is a common task and one does not
> realize how often one does need vertical slicing. eg.: getting the keys
> of dictionary is a vertical slicing, or turning a list into a dict
> involves vertical slicing...
>
> Just out of pure curiosity: Is there a langue that allows vertical and
> horizontal slicing and dicing with the same built-in pattern?
>
> Regards,
> Marco (Eagerly awaiting the cookbook's second edition)
>
>
>

Check out Numeric (for Python). They do lots of slicing anddicing.
APL cetainly has (had?) it as its meat.
I think J is an ASCII-friendly APL variant.

-Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Edward A. Falk C Programming 1 04-04-2013 08:07 PM jazzdman@gmail.com Java 1 03-27-2005 06:56 AM Marco Aschwanden Python 4 08-27-2004 02:41 AM Marco Aschwanden Python 1 08-26-2004 08:47 PM SN Java 4 11-13-2003 05:26 AM

Advertisments