samedi 19 mai 2018

Randomly select subset of all combination in Python

I can construct a list of all combinations of n-length binary values itertools.list(product([0, 1], repeat=n)) when n is small.

1000
0100
0110
1001
 .
 .
 .

How can I randomly select a subset of the list above without first constructing a massive combination list when n is big?

Let say I want to randomly pick 1 million combinations without replacement when n = 30 (2^30 combinations in total)

I looked at an extended function from itertools http://docs.python.org/2/library/itertools.html#recipes

def random_product(*args, **kwds):
    "Random selection from itertools.product(*args, **kwds)"
    pools = map(tuple, args) * kwds.get('repeat', 1)
    return tuple(random.choice(pool) for pool in pools)

but it only returns once at a time. Should I do a loop of this function until I get 1 million of unique combinations? or there is a better way. Thanks!




Aucun commentaire:

Enregistrer un commentaire