permuting several lists (was Re: [Python-Dev] zip() and list-comprehension with commas)

M.-A. Lemburg mal@lemburg.com
Wed, 12 Jul 2000 10:32:00 +0200


Peter Schneider-Kamp wrote:
> 
> Peter Funk wrote:
> >
> > What about calling this function 'permute()'?
> 
> -1 on this: We are not looking for all possible pairings but for the
>             (possibly lazy) equivalent of map(None, list1, list2, ...).
> 
> > BTW:  How comes, that Ping very often invents or introduces very clever
> > ideas and concepts, but also very often chooses unclear names for them?
> > Is it just me not being a native english speaker?
> 
> I still think "zip" is okay, but that's probably my Haskell background.
> And no, I don't think it has to do something with not being a native
> English speaker. It's probably more the context you associate with
> "zip".
> I can't comment on earlier cases, though.

Just for completeness: mx.Tools already has these APIs:

     tuples(sequence) 
         Returns much the same as apply(map,(None,)+tuple(sequence)) does, except that the
         resulting list will always have the length of the first sub-sequence in sequence. The function returns a
         list of tuples (a[0], b[0], c[0],...), (a[1], b[1], c[1],...), ... with missing
         elements being filled in with None. 

         Note that the function is of the single argument type meaning that calling tuples(a,b,c) is the
         same as calling tuples((a,b,c)). tuples() can be used as inverse to lists().

     lists(sequence) 
         Same as tuples(sequence), except that a tuple of lists is returned. Can be used as inverse to tuples().

and these have also proven useful:

     dict(items) 
         Constructs a dictionary from the given items sequence. The sequence items must contain sequence
         entries with at least two values. The first one is interpreted as key, the second one as associated
         object. Remaining values are ignored. 

     irange(object[,indices]) 
         Builds a tuple of tuples (index,object[index]). If a sequence indices is given, the indices
         are read from it. If not, then the index sequence defaults to trange(len(object)). 

         Note that object can be any object that can handle object[index], e.g. lists, tuples, string,
         dictionaries, even your own objects, if they provide a __getitem__-method. This makes very nifty
         constructions possible and extracting items from another sequence becomes a piece of cake. Give it
         a try ! You'll soon love this little function. 

     indices(object) 
         Returns the same as tuple(range(len(object))) -- a tad faster and a lot easier to type.

I'm sure Python could steal a few of those ;-)

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/