What I'd need is to create combinations for two elements a time.
If a list contains: seq = ['A', 'B', 'C']
the output would be com = [['A', 'B'], ['A', 'C'], ['B', 'C']]
all this without itertools.combinations
method.
I used the following code for the permutations. But how could I modify it to make it work with the combinations?
def permute(seq):
if len(seq) <= 1:
perms = [seq]
else:
perms = []
for i in range(len(seq)):
sub = permute(seq[:i]+seq[i+1:])
for p in sub:
perms.append(seq[i:i+1]+p)
return perms
itertools
? – Malfunctioncombinations
is on theitertools
documentation page. Just copy-paste it into your file. – Demarcateitertools.combinations
function returns lexicographic sort order which may be undesirable for lists of integers - iecombinations([1,2,10,3], 3)
yields[1,2,10]
before[1,2,3]
. – Monoceros