Some thougts on cartesian products

Christoph Zwerschke cito at online.de
Sun Jan 22 13:06:46 EST 2006


Alex Martelli schrieb:
> Christoph Zwerschke <cito at online.de> wrote:
>    ...
>> given length. You could get a 6/49 lotto tip with something like:
>>
>> choice(set(range(49)).powerset(6))

> And that would be better than the current random.sample(range(49),6) in
> WHAT ways, exactly...?

You're right, random.sample(range(49),6) does the same and much faster. 
I just didn't think of it (it is new since Python 2.3).

What if you need 12 different tips for your lotto ticket?

s = set(range(49)).powerset(6)
for x in range(10):
     print s.pop()

But the real disadvantage of this idea is the memory consumed and the 
time to set up that memory: set(range(49)).powerset(6) has a cardinality 
of about 13 million entries! You PC would start to swap just for getting 
a lotto tip...

-- Christoph



More information about the Python-list mailing list