user3763437
user3763437

Reputation: 359

Cycle through all possible 3 letter combinations

I am trying to produce all combinations of 3 letter keys from a-z alphabet.

def keyGen(myKey):
    for j in range(0, len(myKey) + 1):
        for subset in itertools.combinations(myKey, 3):
            print(subset)

But I don't get all combinations:

('a', 'b', 'c')
('a', 'b', 'd')
('a', 'b', 'e')
('a', 'b', 'f')
('a', 'b', 'g')
('a', 'b', 'h')
('a', 'b', 'i')
('a', 'b', 'j')
('a', 'b', 'k')
...............
('v', 'y', 'z')
('w', 'x', 'y')
('w', 'x', 'z')
('w', 'y', 'z')
('x', 'y', 'z')

As you can see, after selecting first letter, it does not go downwards in alphabet but only upwards. It won't choose aba, eda, eac etc. I can't figure out how to do ALL possible combinations.

Upvotes: 0

Views: 384

Answers (1)

sundar nataraj
sundar nataraj

Reputation: 8702

You can try for itertools.product

itertools.product(mykeys, repeat=3)

Upvotes: 3

Related Questions