Re: How to generically transform a list?

Discussion in 'Python' started by Marco Aschwanden, Aug 27, 2004.

  1. 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)
    Marco Aschwanden, Aug 27, 2004
    #1
    1. Advertising

  2. 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, Aug 27, 2004
    #2
    1. Advertising

  3. 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
    Roel Schroeven, Aug 27, 2004
    #3
  4. Roel Schroeven <> 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
    Alex Martelli, Aug 27, 2004
    #4
  5. Alex Martelli wrote:

    > Roel Schroeven <> 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
    Roel Schroeven, Aug 27, 2004
    #5
  6. 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
    Scott David Daniels, Aug 28, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SN
    Replies:
    4
    Views:
    387
    Tony Dahlman
    Nov 13, 2003
  2. VisionSet

    generics - overriding generically

    VisionSet, Jan 8, 2006, in forum: Java
    Replies:
    14
    Views:
    750
    Oliver Wong
    Jan 9, 2006
  3. Replies:
    0
    Views:
    510
  4. Marco Aschwanden

    How to generically transform a list?

    Marco Aschwanden, Aug 26, 2004, in forum: Python
    Replies:
    4
    Views:
    288
    Arthur Rambo
    Aug 27, 2004
  5. Marco Aschwanden

    Re: How to generically transform a list?

    Marco Aschwanden, Aug 26, 2004, in forum: Python
    Replies:
    1
    Views:
    304
    Kent Johnson
    Aug 26, 2004
Loading...

Share This Page