I have three lists of values (numbers and letters) and I want to write a program that makes combinations of one of each lists randomly.
I found a code making all possible combinations of values and I thought that might be a good base, but now I don t know how to continue. Who can help me?
Here the code I have
import itertools
square = [a, s, d, f, g, h, j, k, l ]
circle = [w, e, r, t, z, u, i, o, p ]
line = [y, x, c, v, b, n, m ]
radiusshape = [1, 2, 3, 4, 5, 6, 7, 8, 9 ]
for L in range(0, len(stuff)+1):
for subset in itertools.combinations(stuff, L):
print(subset)
You can use random.sample to draw k random samples from your generated cartesian product
# where k is number of samples to generate
samples = random.sample(itertools.product(square, circle, line, radiusshape), k)
For example
>>> a = [1, 2, 3, 4]
>>> b = ['a', 'b', 'c', 'd']
>>> c = ['foo', 'bar']
>>> random.sample(set(itertools.product(a,b,c)), 5)
[(1, 'c', 'foo'),
(4, 'c', 'bar'),
(1, 'd', 'bar'),
(2, 'a', 'foo'),
(2, 'd', 'foo')]
You can use the random.choice() function to select a random element from a list, so just use it on all 4 lists:
from random import choice
combination = (choice(square), choice(circle), choice(line), choice(radiusshape))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With