B
What you request is not permutations, since in the permutations what you would have are always the same 5 elements (we say the numbers 3,3,4,4,5) reordered of all possible forms, but always two threes, two fours and a five.Instead judging by your example, what you are looking for is simply that the first element of the list is filled with a figure to choose between (3.4,5), the second element the same, the third equal, etc... So you could get for example as a result [3, 3, 3, 3, 3] but also [3, 3, 3, 3, 4]etc.It is not clear to me if you want to get these "at random" elements (in which case it would be enough to choose a random figure between (3.4,5) for each position on the list) or you really want all possible combinations.Get cases randomlyimport random
def dame_un_caso(conjunto, n=5):
lista = []
for _ in range(n):
lista.append(random.choice(list(conjunto)))
return lista
Every time you call dame_un_caso(conjunto) You will be returned a list of 5 randomly chosen elements within the set. For example:>>> dame_un_caso({3,4,5})
[3, 5, 4, 4, 5]
Get all casesThe number of combinations in this case is not very high, but depending on the size of the set and the list can be. Yeah. N is the size of the list and M is the size of the set, the number of combinations would be M**N (because you have M possible choices for the first element and for each of them other M for the second, etc. with what you finally have M*M*M*...*M repeated N times).In your case N=5, M=3 comes out 3**5 which are 241 combinations.If you want them all, the operation you really need is the Cartesian product of the set (3.4,5) by itself, 5 times. This operation makes you. itertools.productLike this:from itertools import product
l = list(product({3,4,5}, repeat=5))
If you look len(l) You'll see 241, which matches what's expected. If you show the elements l (in this order):[(3, 3, 3, 3, 3),
(3, 3, 3, 3, 4),
(3, 3, 3, 3, 5),
(3, 3, 3, 4, 3),
(3, 3, 3, 4, 4),
(3, 3, 3, 4, 5),
(3, 3, 3, 5, 3),
(3, 3, 3, 5, 4),
(3, 3, 3, 5, 5),
(3, 3, 4, 3, 3),
(3, 3, 4, 3, 4),
(3, 3, 4, 3, 5),
... etc