-2

I am needing a code that calculates every possible combination of a values and b columns.

'''
aaa
aab
aac
aad
aba
abb
abc
abd
aca
acb
acc
acd
ada
adb
adc
add
baa
bab
bac
bad
bba
bbb
bbc
bbd
bca
bcb
bcc
bcd
bda
bdb
bdc
bdd
caa
cab
cac
cad
cba
cbb
cbc
cbd
cca
ccb
ccc
ccd
cda
cdb
cdc
cdd
daa
dab
dac
dad
dba
dbb
dbc
dbd
dca
dcb
dcc
dcd
dda
ddb
ddc
ddd
'''

I can easily do this but the problem is that the code can only do this, I need it to take in more variables and more columns than this but I haven't got any idea of how to do that.

I have found code that returns:

['a', 'b', 'c'],
 ['a', 'b', 'd'],
 ['a', 'c', 'd'],
 ['b', 'c', 'd'],

but this is not in the same order that am able to use.

  • Have you tried using `itertools.combinations` or another function from `itertools`? – mkrieger1 May 23 '22 at 13:40
  • I've never really even touched those so I've got no idea of how to use it with just a glance, I'll have to look into it a bit, thanks. – Clonnan May 23 '22 at 13:45
  • I'm having trouble getting "combinations" to return what I need, it is returning: ['abc','abd','abe','acd','ace','ade','bcd','bce','bde','cde'] But I need: ['aaa','aab','aac','aad','aae','aba',...] – Clonnan May 23 '22 at 14:05

1 Answers1

0

You are looking for itertools.product

from itertools import product

for i in product('ABCD', repeat=3):
    print(''.join(i))

Output:

AAA
AAB
AAC
AAD
ABA
ABB
ABC
ABD
ACA
ACB
ACC
ACD
ADA
ADB
ADC
ADD
BAA
BAB
BAC
BAD
BBA
BBB
BBC
BBD
BCA
BCB
BCC
BCD
BDA
BDB
BDC
BDD
CAA
CAB
CAC
CAD
CBA
CBB
CBC
CBD
CCA
CCB
CCC
CCD
CDA
CDB
CDC
CDD
DAA
DAB
DAC
DAD
DBA
DBB
DBC
DBD
DCA
DCB
DCC
DCD
DDA
DDB
DDC
DDD
alec_djinn
  • 10,104
  • 8
  • 46
  • 71