Using itertools.combinations, the following works great:
for letter_group in itertools.combinations(alphabet, 5):
print letter group
Now I want to have the first 2 of the 5 items in the combinations constant. Is there a way to do that?
For example, the output would then be:
(a,b,c,d,e)
(a,b,e,f,g)
...
(a,b,x,y,z)
Well, I think what you're trying to do is make 5-tuples where the first 2 are specified and the last 3 are from an iterable called alphabet
, but where the last 3 can't reuse the first two.
How about this:
start = ('a', 'b')
for ending in itertools.combinations(set(alphabet) - set(start), 3):
print start + ending
Just make your alphabet
without the first two letters, do combinations of the rest, then contact ['a', 'b'] with the result, the runnable code:
# -*- coding: utf-8 -*-
import string
import itertools
alphabet = string.letters[:26]
prefix = ['a', 'b']
for letter_group in itertools.combinations(alphabet[len(prefix):], 3):
print prefix + list(letter_group)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.