OK 400
OK 400

Reputation: 831

Combinations with repetitions

With itertools.combinations (or any other function) I want to know how much combinations can be done with the following requirements:

So each array is:

A = ['A', 'A', 'A', 'A', 'A']
B = ['B', 'B', 'B', 'B', 'B', 'B']
C = ['C', 'C', 'C', 'C', 'C', 'C', 'C']
D = ['D', 'D', 'D', 'D', 'D', 'D', 'D', 'D']

For example, a combination would be: ('A', 'A', 'A', 'A', 'A') or ('A', 'D', 'D', 'B', 'C'). Order does NOT matter. How could I do that?

Upvotes: 0

Views: 60

Answers (2)

fountainhead
fountainhead

Reputation: 3722

Since you had mentioned that order does not matter, I think that for your example, the correct answer should produce only 56 combinations.

You can use this:

from itertools import combinations_with_replacement

combi = list(combinations_with_replacement(set(A+B+C+D), 5))

For your example, with this solution, print(len(combi)) and print(combi) produce the following outputs:

56

[('C', 'C', 'C', 'C', 'C'),
 ('C', 'C', 'C', 'C', 'A'),
 ('C', 'C', 'C', 'C', 'B'),
 ('C', 'C', 'C', 'C', 'D'),
 ('C', 'C', 'C', 'A', 'A'),
 ('C', 'C', 'C', 'A', 'B'),
 ('C', 'C', 'C', 'A', 'D'),
 ('C', 'C', 'C', 'B', 'B'),
 ('C', 'C', 'C', 'B', 'D'),
 ('C', 'C', 'C', 'D', 'D'),
 ('C', 'C', 'A', 'A', 'A'),
 ('C', 'C', 'A', 'A', 'B'),
 ('C', 'C', 'A', 'A', 'D'),
 ('C', 'C', 'A', 'B', 'B'),
 ('C', 'C', 'A', 'B', 'D'),
 ('C', 'C', 'A', 'D', 'D'),
 ('C', 'C', 'B', 'B', 'B'),
 ('C', 'C', 'B', 'B', 'D'),
 ('C', 'C', 'B', 'D', 'D'),
 ('C', 'C', 'D', 'D', 'D'),
 ('C', 'A', 'A', 'A', 'A'),
 ('C', 'A', 'A', 'A', 'B'),
 ('C', 'A', 'A', 'A', 'D'),
 ('C', 'A', 'A', 'B', 'B'),
 ('C', 'A', 'A', 'B', 'D'),
 ('C', 'A', 'A', 'D', 'D'),
 ('C', 'A', 'B', 'B', 'B'),
 ('C', 'A', 'B', 'B', 'D'),
 ('C', 'A', 'B', 'D', 'D'),
 ('C', 'A', 'D', 'D', 'D'),
 ('C', 'B', 'B', 'B', 'B'),
 ('C', 'B', 'B', 'B', 'D'),
 ('C', 'B', 'B', 'D', 'D'),
 ('C', 'B', 'D', 'D', 'D'),
 ('C', 'D', 'D', 'D', 'D'),
 ('A', 'A', 'A', 'A', 'A'),
 ('A', 'A', 'A', 'A', 'B'),
 ('A', 'A', 'A', 'A', 'D'),
 ('A', 'A', 'A', 'B', 'B'),
 ('A', 'A', 'A', 'B', 'D'),
 ('A', 'A', 'A', 'D', 'D'),
 ('A', 'A', 'B', 'B', 'B'),
 ('A', 'A', 'B', 'B', 'D'),
 ('A', 'A', 'B', 'D', 'D'),
 ('A', 'A', 'D', 'D', 'D'),
 ('A', 'B', 'B', 'B', 'B'),
 ('A', 'B', 'B', 'B', 'D'),
 ('A', 'B', 'B', 'D', 'D'),
 ('A', 'B', 'D', 'D', 'D'),
 ('A', 'D', 'D', 'D', 'D'),
 ('B', 'B', 'B', 'B', 'B'),
 ('B', 'B', 'B', 'B', 'D'),
 ('B', 'B', 'B', 'D', 'D'),
 ('B', 'B', 'D', 'D', 'D'),
 ('B', 'D', 'D', 'D', 'D'),
 ('D', 'D', 'D', 'D', 'D')]

Upvotes: 1

mhhabib
mhhabib

Reputation: 3121

Add all the four list into one list then set parameter into combinations function. You can also add all the list into the combination function.

from itertools import combinations

A = ['A', 'A', 'A', 'A', 'A']
B = ['B', 'B', 'B', 'B', 'B', 'B']
C = ['C', 'C', 'C', 'C', 'C', 'C', 'C']
D = ['D', 'D', 'D', 'D', 'D', 'D', 'D', 'D']

comb = combinations(A+B+C+D, 5) 
for c in comb:
  print(c)

Upvotes: 1

Related Questions