Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19097

Re: unzip function?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!newsfeed.tele2net.at!news-hub.siol.net!newsfeed.t-com.hr!garrison.bnet.hr!gregory.BNet.hr!busola.homelinux.net!not-for-mail
From Hrvoje Niksic <hniksic@xemacs.org>
Newsgroups comp.lang.python
Subject Re: unzip function?
Date Wed, 18 Jan 2012 19:01:10 +0100
Organization B.net Hrvatska d.o.o.
Lines 41
Message-ID <87aa5khoux.fsf@xemacs.org> (permalink)
References <mailman.4828.1326897240.27778.python-list@python.org>
NNTP-Posting-Host cpe-109-60-90-39.zg3.cable.xnet.hr
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace gregory.bnet.hr 1326910502 21776 109.60.90.39 (18 Jan 2012 18:15:02 GMT)
X-Complaints-To abuse@globalnet.hr
NNTP-Posting-Date Wed, 18 Jan 2012 18:15:02 +0000 (UTC)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)
Cancel-Lock sha1:vydqhLgdLMnioXGFlSIDblxbtxA=
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19097

Show key headers only | View raw


Neal Becker <ndbecker2@gmail.com> writes:

> python has builtin zip, but not unzip
>
> A bit of googling found my answer for my decorate/sort/undecorate problem:
>
> a, b = zip (*sorted ((c,d) for c,d in zip (x,y)))
>
> That zip (*sorted...
>
> does the unzipping.
>
> But it's less than intuitively obvious.
>
> I'm thinking unzip should be a builtin function, to match zip.

"zip" and "unzip" are one and the same since zip is inverse to itself:

>>> [(1, 2, 3), (4, 5, 6)]
[(1, 2, 3), (4, 5, 6)]
>>> zip(*_)
[(1, 4), (2, 5), (3, 6)]
>>> zip(*_)
[(1, 2, 3), (4, 5, 6)]
>>> zip(*_)
[(1, 4), (2, 5), (3, 6)]

What you seem to call unzip is simply zip with a different signature,
taking a single argument:

>>> def unzip(x):
...   return zip(*x)
...
>>> [(1, 2, 3), (4, 5, 6)]
[(1, 2, 3), (4, 5, 6)]
>>> unzip(_)
[(1, 4), (2, 5), (3, 6)]
>>> unzip(_)
[(1, 2, 3), (4, 5, 6)]
>>> unzip(_)
[(1, 4), (2, 5), (3, 6)]

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

unzip function? Neal Becker <ndbecker2@gmail.com> - 2012-01-18 09:33 -0500
  Re: unzip function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-18 15:27 +0000
    Re: unzip function? Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-01-18 11:20 -0500
      Re: unzip function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-18 23:20 +0000
        Re: unzip function? Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-01-19 10:56 -0500
        RE: unzip function? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-02-03 00:53 +0000
  Re: unzip function? Hrvoje Niksic <hniksic@xemacs.org> - 2012-01-18 19:01 +0100

csiph-web