unzip function?
Hrvoje Niksic
hniksic at xemacs.org
Wed Jan 18 13:01:10 EST 2012
Neal Becker <ndbecker2 at 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)]
More information about the Python-list
mailing list