Some thougts on cartesian products

Christoph Zwerschke cito at online.de
Sun Jan 22 21:21:59 EST 2006


Bryan Olson wrote:
> What you seem to want is easy enough:
>   [a + b for a in 'hello' for b in 'world']

Yes, but 'hello'*'world' is easier.

And how would you write '01'**8 ? This should give you all bytes in 
binary code.

> And maybe you want the result to be a generator:
>     (a + b for a in 'hello' for b in 'world')

That may be the main problem to decide whether the cartesian product 
should return a generator or a list. A list would be more intuitive and 
can be accessed directly. For instance,

('01'**8)[42]

would give you the binary code of 42 (though in an inefficient way).
But because the inflationary character of cartesian products, a 
generator would be also desirable (though you wouldn't expect '01'**2 to 
be a generator, since '01'*2 isn't a generator either).

> New language features should be widely useful, and difficult or
> awkward to code in Python as it is. All-combinations-of-sequences
> is trivial to code and rarely needed.

That's the other problem. The uses cases (like the password cracker 
example) are very limited and in these cases you can either write nested 
loops or write your own cartesian product.

-- Christoph

BTW: What is the shortest way to get the binary representation of a 
number in Python? Is there really not something like itoa() anywhere in 
the standard libs?



More information about the Python-list mailing list