Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > inverse of the zip function

Reply
Thread Tools

inverse of the zip function

 
 
David C. Fox
Guest
Posts: n/a
 
      07-29-2003
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

 
Reply With Quote
 
 
 
 
David C. Fox
Guest
Posts: n/a
 
      07-29-2003
Terry Reedy wrote:

> "David C. Fox" <(E-Mail Removed)> 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=e...mp.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

 
Reply With Quote
 
 
 
 
Simon Burton
Guest
Posts: n/a
 
      07-29-2003
On Tue, 29 Jul 2003 22:06:20 +0000, Raymond Hettinger wrote:

> "David C. Fox" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Simon Burton
Guest
Posts: n/a
 
      07-29-2003
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" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      07-29-2003
> >> 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





 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      07-29-2003
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
 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      07-30-2003
> 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


 
Reply With Quote
 
 
 
Reply

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inverse of dict(zip(x,y)) lone_eagle Python 22 03-05-2009 09:04 PM
inverse function, how? Pasacco VHDL 3 11-29-2006 05:12 PM
Possibility to add a zip-file to a new zip-file with "add to zip" (right-click) ?? erikkie@casema.nl Computer Support 4 06-26-2006 12:18 AM
Asking inverse() function for List and ArrayList =?iso-8859-1?B?bW9vcJk=?= Java 11 12-13-2005 04:32 PM
Query about tan inverse function jahaya@gmail.com VHDL 1 07-17-2005 01:27 PM



Advertisments