Created
March 4, 2012 01:10
-
-
Save dougwt/1969743 to your computer and use it in GitHub Desktop.
Python: All Possible Combinations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def combinations(n, list, combos=[]): | |
# initialize combos during the first pass through | |
if combos is None: | |
combos = [] | |
if len(list) == n: | |
# when list has been dwindeled down to size n | |
# check to see if the combo has already been found | |
# if not, add it to our list | |
if combos.count(list) == 0: | |
combos.append(list) | |
combos.sort() | |
return combos | |
else: | |
# for each item in our list, make a recursive | |
# call to find all possible combos of it and | |
# the remaining items | |
for i in range(len(list)): | |
refined_list = list[:i] + list[i+1:] | |
combos = combinations(n, refined_list, combos) | |
return combos |
I have a question about combination. I made a program in python and I ll select two enzymes from 14 restriction enzymes in dictionary. But i didnt exactly work on it. pls help me out
I mean I ll do (14)
2
You can write it in only one line:
list(itertools.combinations([1, 2, 3], 2))
Thank you for your script
This results in TLE in most cases of programs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As @mohanadkaleia indicated, combinations method from itertools is more convenient.