Re: Reverse zip() ?

2008-12-03 Thread Casey McGinty
> The corner case is when dealing with empty lists and there aren't > enough items to unpack. > > Another solution to zip(), with a slightly different behaviour for conner cases >>> a = (1,2,3) >>> b = (1,2,3) >>> c = (1,2,3,4) >>> zip(a,b) [(1, 1), (2, 2), (3, 3)] >>> map(None,a,b) [(1, 1),

Re: Reverse zip() ?

2008-12-03 Thread Benjamin Kaplan
On Wed, Dec 3, 2008 at 11:19 AM, Andreas Waldenburger <[EMAIL PROTECTED]>wrote: > On Wed, 3 Dec 2008 07:08:52 -0800 (PST) Janto Dreijer > <[EMAIL PROTECTED]> wrote: > > > I'd like to point out that since your where thinking in terms of > > matplotlib, you might actually find numpy's own transpose

Re: Reverse zip() ?

2008-12-03 Thread Andreas Waldenburger
On Wed, 3 Dec 2008 07:08:52 -0800 (PST) Janto Dreijer <[EMAIL PROTECTED]> wrote: > I'd like to point out that since your where thinking in terms of > matplotlib, you might actually find numpy's own transpose useful, > instead of using zip(*seq) :) > This was, of course, to be expected. :) Whenev

Re: Reverse zip() ?

2008-12-03 Thread Janto Dreijer
I'd like to point out that since your where thinking in terms of matplotlib, you might actually find numpy's own transpose useful, instead of using zip(*seq) :) untested: t = linspace(0,2*pi*3) seq = asarray(zip(t, sin(t))) t, y = seq.T # or seq.transpose() or numpy.transpose(seq) pylab.plot(t,y

Re: Reverse zip() ?

2008-12-03 Thread Andreas Waldenburger
On Wed, 3 Dec 2008 02:11:51 -0800 (PST) alex23 <[EMAIL PROTECTED]> wrote: > On Dec 3, 6:51 pm, Andreas Waldenburger <[EMAIL PROTECTED]> wrote: > > On Tue, 02 Dec 2008 18:16:13 -0800 Bryan Olson > > > zip as its own inverse might be even easier to comprehend if we > > > call zip by its more traditi

Re: Reverse zip() ?

2008-12-03 Thread alex23
On Dec 3, 6:51 pm, Andreas Waldenburger <[EMAIL PROTECTED]> wrote: > On Tue, 02 Dec 2008 18:16:13 -0800 Bryan Olson > > zip as its own inverse might be even easier to comprehend if we call > > zip by its more traditional name, "transpose". > > Sounds like a Py4k change to me. Nah, just add the fol

Re: Reverse zip() ?

2008-12-03 Thread Andreas Waldenburger
On Tue, 02 Dec 2008 18:16:13 -0800 Bryan Olson <[EMAIL PROTECTED]> wrote: > zip as its own inverse might be even easier to comprehend if we call > zip by its more traditional name, "transpose". > Sounds like a Py4k change to me. /W -- My real email address is constructed by swapping the domain

Re: Reverse zip() ?

2008-12-02 Thread Stefan Behnel
Zac Burns wrote: > More succinct failure: > > keys, values = zip(*{}.iteritems()) Simple fix: some_iterable = {}.iteritems() keys, values = zip(*list(some_iterable)) or: keys, values = zip(*(some_iterable if isinstance(some_iterable, (list, tuple))

Re: Reverse zip() ?

2008-12-02 Thread Bryan Olson
John Machin wrote: Here's a version that makes it slightly easier to comprehend: Q: I know how to zip sequences together: | >>> a = (1, 2, 3) | >>> b = (4, 5, 6) | >>> z = zip(a, b) | >>> z | [(1, 4), (2, 5), (3, 6)] but how do I reverse the process? A: Use zip()! | >>> a2, b2 = zip(*z) | >>> a

Re: Reverse zip() ?

2008-12-02 Thread Bryan Olson
Zac Burns wrote: There is a problem with this however, which prompted me to actually write an unzip function. One might expect to be able to do something like so (pseudocode)... def filesAndAttributes(): files = walk() attributes = [attr(f) for f in files] return zip(files, attributes)

Re: Reverse zip() ?

2008-12-02 Thread Zac Burns
More succinct failure: keys, values = zip(*{}.iteritems()) -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games On Tue, Dec 2, 2008 at 4:47 PM, Zac Burns <[EMAIL PROTECTED]> wrote: > There is a problem with this however, which prompted me to actual

Re: Reverse zip() ?

2008-12-02 Thread Zac Burns
There is a problem with this however, which prompted me to actually write an unzip function. One might expect to be able to do something like so (pseudocode)... def filesAndAttributes(): files = walk() attributes = [attr(f) for f in files] return zip(files, attributes) files, attributes

Re: Reverse zip() ?

2008-12-02 Thread John Machin
On Dec 3, 7:12 am, Stefan Behnel <[EMAIL PROTECTED]> wrote: > Andreas Waldenburger wrote: > > we all know about the zip builtin that combines several iterables into > > a list of tuples. > > > I often find myself doing the reverse, splitting a list of tuples into > > several lists, each correspondi

Re: Reverse zip() ?

2008-12-02 Thread Andreas Waldenburger
On Tue, 02 Dec 2008 21:12:19 +0100 Stefan Behnel <[EMAIL PROTECTED]> wrote: > Andreas Waldenburger wrote: > > [snip] > > This is of course trivial to do via iteration or listcomps, BUT, I > > was wondering if there is a function I don't know about that does > > this nicely? > > I think you're aski

Re: Reverse zip() ?

2008-12-02 Thread Stefan Behnel
Andreas Waldenburger wrote: > we all know about the zip builtin that combines several iterables into > a list of tuples. > > I often find myself doing the reverse, splitting a list of tuples into > several lists, each corresponding to a certain element of each tuple > (e.g. matplotlib/pyplot needs

Reverse zip() ?

2008-12-02 Thread Andreas Waldenburger
Hi all, we all know about the zip builtin that combines several iterables into a list of tuples. I often find myself doing the reverse, splitting a list of tuples into several lists, each corresponding to a certain element of each tuple (e.g. matplotlib/pyplot needs those, rather than lists of po