Possible Duplicate:
Get the cartesian product of a series of lists in Python
I'm trying to figure out some logic that I just can't wrap my head around. Say I have the following data structure:
letters = (
('b','c'),
('a','e','ee'),
('d','f'),
('e','y'),
)
How would I iterate through this to get every possible string combination:
bade
cade
bede
cede
beede
ceede
bafe
cafe
befe
cefe
beefe
ceefe
bady
cady
bedy
cedy
beedy
ceedy
bafy
cafy
befy
cefy
beefy
ceefy
I'd use itertools.product()
:
for l in itertools.product(*letters):
print ''.join(l)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With