inverse of the zip function

Discussion in 'Python' started by David C. Fox, Jul 29, 2003.

  1. David C. Fox

    David C. Fox Guest

    Is there a function which takes a list of tuples and returns a list of
    lists made up of the first element of each tuple, the second element of
    each tuple, etc.?

    In other words, the the inverse of the built-in zip function?

    David
     
    David C. Fox, Jul 29, 2003
    #1
    1. Advertising

  2. David C. Fox

    David C. Fox Guest

    Terry Reedy wrote:

    > "David C. Fox" <> wrote in message
    > news:9sBVa.12998$o%2.6289@sccrnsc02...
    >
    >>Is there a function which takes a list of tuples and returns a list

    >
    > of
    >
    >>lists made up of the first element of each tuple, the second element

    >
    > of
    >
    >>each tuple, etc.?
    >>
    >>In other words, the the inverse of the built-in zip function?

    >
    >
    > Go to
    > http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&group=comp.lang.python
    > enter 'zip inverse', and check search Python only.
    >
    > TJR
    >
    >


    Thanks. I've gotten so used to reading this group with Mozilla Mail
    that I forgot about google groups.

    David
     
    David C. Fox, Jul 29, 2003
    #2
    1. Advertising

  3. David C. Fox

    Simon Burton Guest

    On Tue, 29 Jul 2003 22:06:20 +0000, Raymond Hettinger wrote:

    > "David C. Fox" <> wrote in message
    > news:9sBVa.12998$o%2.6289@sccrnsc02...
    >> Is there a function which takes a list of tuples and returns a list of
    >> lists made up of the first element of each tuple, the second element of
    >> each tuple, etc.?
    >>
    >> In other words, the the inverse of the built-in zip function?

    >
    > When used with the * operator, zip() is its own inverse:
    >


    This (obviously) doesn't work when z has length 0 or 2.
    I don't quite understand why zip is overloaded ...

    Oh, hang on, it does work for length 2! that's neat-o,
    and perhaps that's why zip was extended. Is it a functional programming
    convention, i wonder.

    Simon.
     
    Simon Burton, Jul 29, 2003
    #3
  4. David C. Fox

    Simon Burton Guest

    On Wed, 30 Jul 2003 08:31:47 +1000, Simon Burton wrote:

    > On Tue, 29 Jul 2003 22:06:20 +0000, Raymond Hettinger wrote:
    >
    >> "David C. Fox" <> wrote in message
    >> news:9sBVa.12998$o%2.6289@sccrnsc02...
    >>> Is there a function which takes a list of tuples and returns a list of
    >>> lists made up of the first element of each tuple, the second element of
    >>> each tuple, etc.?
    >>>
    >>> In other words, the the inverse of the built-in zip function?

    >>
    >> When used with the * operator, zip() is its own inverse:
    >>


    OK, i think i see now. it's swapping rows<->columns, and might help this
    other guy with his gridcontrols. But zip() should return (). No?

    Simon.
     
    Simon Burton, Jul 29, 2003
    #4
  5. > >> In other words, the the inverse of the built-in zip function?
    > >
    > > When used with the * operator, zip() is its own inverse:
    > >

    >
    > This (obviously) doesn't work when z has length 0 or 2.
    > I don't quite understand why zip is overloaded ...
    >
    > Oh, hang on, it does work for length 2! that's neat-o,
    > and perhaps that's why zip was extended. Is it a functional programming
    > convention, i wonder.
    >
    > Simon.



    There is no special extension to zip().
    It just happens to be one of those functions
    like int.__neg__() that is closely related to
    its own inverse.

    * or apply() serve only to break a list into
    individual arguments. So, transpose() can
    be defined like this:

    def transpose(mat):
    return zip(*mat)

    The transpose() is its own inverse for rectangular
    matrices represented as lists of tuples.


    Raymond Hettinger
     
    Raymond Hettinger, Jul 29, 2003
    #5
  6. Raymond Hettinger wrote:

    >> >> In other words, the the inverse of the built-in zip function?
    >> >
    >> > When used with the * operator, zip() is its own inverse:

    > There is no special extension to zip().
    > It just happens to be one of those functions
    > like int.__neg__() that is closely related to
    > its own inverse.
    >
    > * or apply() serve only to break a list into
    > individual arguments. So, transpose() can
    > be defined like this:


    I understand why it works as inverse when *<list> creates a argument list of
    list element. But don't understand why * works that way in this context.
    Does ** do this for maps and keywordargs, too? Hey, this is python - lets
    try:
    >>> def foo(a=None, b=None):

    .... pass
    ....
    >>> foo(a=10, b=20)
    >>> foo(**{'a':10, 'b':20})
    >>>


    Coooool. Where is that documented? Never stumbled across it so far!

    Diez
     
    Diez B. Roggisch, Jul 30, 2003
    #6
  7. > I understand why it works as inverse when *<list> creates a argument list of
    > list element. But don't understand why * works that way in this context.
    > Does ** do this for maps and keywordargs, too? Hey, this is python - lets
    > try:
    > >>> def foo(a=None, b=None):

    > ... pass
    > ...
    > >>> foo(a=10, b=20)
    > >>> foo(**{'a':10, 'b':20})
    > >>>

    >
    > Coooool. Where is that documented? Never stumbled across it so far!


    http://www.python.org/dev/doc/devel/ref/calls.html


    Raymond Hettinger
     
    Raymond Hettinger, Jul 30, 2003
    #7
    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. Replies:
    1
    Views:
    842
    David Bishop
    Jul 17, 2005
  2. =?iso-8859-1?B?bW9vcJk=?=

    Asking inverse() function for List and ArrayList

    =?iso-8859-1?B?bW9vcJk=?=, Dec 12, 2005, in forum: Java
    Replies:
    11
    Views:
    680
    andreaz
    Dec 13, 2005
  3. Pasacco

    inverse function, how?

    Pasacco, Nov 29, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,155
    Pasacco
    Nov 29, 2006
  4. Edward Hua
    Replies:
    2
    Views:
    524
    Thomas Lumley
    Oct 13, 2005
  5. lone_eagle

    Inverse of dict(zip(x,y))

    lone_eagle, Mar 4, 2009, in forum: Python
    Replies:
    22
    Views:
    771
    Carl Banks
    Mar 5, 2009
Loading...

Share This Page