I am trying to solve the general problem of getting unique combinations from a list in Python
Mathematically from https://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html I see that the formula for the number of combinations is n!/r!(nr)! , where n is the length of the sequence and r is the number to be selected.
As shown in the following python, where n is 4 and r is 2:
lst = 'ABCD' result = list(itertools.combinations(lst, len(lst)/2)) print len(result) 6
Below is a helper function to display the problem that I have:
def C(lst): l = list(itertools.combinations(sorted(lst), len(lst)/2)) s = set(l) print 'actual', len(l), l print 'unique', len(s), list(s)
If I ran this from iPython, I can name it like this:
In [41]: C('ABCD') actual 6 [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')] unique 6 [('B', 'C'), ('C', 'D'), ('A', 'D'), ('A', 'B'), ('A', 'C'), ('B', 'D')] In [42]: C('ABAB') actual 6 [('A', 'A'), ('A', 'B'), ('A', 'B'), ('A', 'B'), ('A', 'B'), ('B', 'B')] unique 3 [('A', 'B'), ('A', 'A'), ('B', 'B')] In [43]: C('ABBB') actual 6 [('A', 'B'), ('A', 'B'), ('A', 'B'), ('B', 'B'), ('B', 'B'), ('B', 'B')] unique 2 [('A', 'B'), ('B', 'B')] In [44]: C('AAAA') actual 6 [('A', 'A'), ('A', 'A'), ('A', 'A'), ('A', 'A'), ('A', 'A'), ('A', 'A')] unique 1 [('A', 'A')]
What I want to get is a unique count, as shown above, but the execution of combinations and then set does not scale. Since, when the length lst , which n increases, it slows down as the combinations get bigger and bigger.
Is there a way to use math or Python tricks to solve the problem of counting unique combinations?