I think I came up with a solution, but please correct me if it is not. I would also like to see if there are more elegant solutions.
First, I came up with the total number of combinations. All combinations without replacement are n! / R! (Nr)! and with replacement equal (m + s-1)! / s! (m-1)! where m and n are the number of elements to choose from, and r and s are the number of elements that you actually select. Since I know the common elements that I want in each combination (let's call it a cap), I find the number of combinations for 0 without changing the type (n = 0) and for the "cap" type of replacement (m = 3) and multiply these numbers together . Then add to this the number of combinations for 1 of the non-replacement type (n = 1) multiplied by the cap-1 combination of the replacement type (m = 2). Do this until you finally add combinations for the โcapโ type of replacement (n = 3) multiplied by 0 type of replacement (m = 0) (thanks @ Andrรฉ Nicolas). Code for the number of combinations below.
import itertools from math import factorial as fact norep = ['A','B','C'] rep = ['1','2','3'] cap = 3
In this example, the number of combos is 38. Then I wanted to print all the combinations. For this, I defined combinations for all substitutions, all without replacement, and any combination of the two, for example. n = 0, m = 3, n = 1, m = 2; etc .. Here is what I came up with:
for i in range(cap+1): norepcomb = [j for j in itertools.combinations(norep,i)] repcomb = [k for k in itertools.combinations_with_replacement(rep,cap-i)] for l in itertools.product(norepcomb,repcomb): print list(itertools.chain.from_iterable(l))
To include none , I would simply include none in my list for replacement combinations. I would like to receive a response to this, especially if there is a better solution or if it does not work, as I think. Thanks!
source share