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

Peter Funk
Wed, 12 Jul 2000 09:52:42 +0200 (MEST)


Barry A. Warsaw:
> >>>>> "KY" == Ka-Ping Yee <> writes:
>     KY> I like this idea.  This whole parallel iteration thing could
>     KY> be solved with a single built-in and no syntax changes.
>     KY>     for x, y in parallel([10, 20, 30], [1, 2]):
>     KY> Or "zip", since you're zipping the lists together (i foresee
>     KY> potential geometric or concurrency-related interpretations of
>     KY> "parallel"):
> Definitely +1 on concept.  The nice bit is that parallel() or zip()
> can return objects that iterate efficiently.  -0 on zip(), +0 on
> parallel(), +0 on making them builtins.

+1 on the concept.
-1 on calling it 'zip' (I was first thinking of Info ZIP and zlib).
-0 on calling it 'parallel' (makes me thinking of threads and processes).

What about calling this function 'permute()'?  
Here is a strawman to play with:

def permute(*args):
    """takes several sequences as arguments and returns a list of tuples,
    where each tuple contains an element of each sequence permutated 
    through all possible combinations."""
    if len(args) == 1:
        return map(lambda x:(x,), args[0])
        res = []; cdr = apply(permute, args[1:])
        for left in args[0]:
            for right in cdr:
                res.append((left,) + right)
        return res        

for x, y in permute([10, 20, 30], [1, 2]):
    print "x, y =", x, y

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?

Regards, Peter