I have a list that looks like:
A
B
C
D
E
F
G
How do I solve this to find all combinations for 3 digits. The same letter cannot be used in same row.
ABC
ABD
ABE
ABF
ABG
AGB
E.g something like...:
x = ['a','b','c','d','e']
n = 3
import itertools
aa = [list(comb) for i in range(1, n+2) for comb in itertools.combinations(x, i)]
print(aa)
This does not give desired input:
[['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'], ['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e'], ['c'
To understand how the solution process works, try the following:
combinations works on strings not lists, so you should first turn it into a string using:
''.join(x)
OUTPUT
Or as a one-liner:
The Python Standard Library itertools already has the functionality you are trying to implement. Also you are using it in your code (funnily).
itertools.combinations(a,3)
returns all 3-combinations of the a. To convert that to "list of list" you should use.extend()
as follows;