I am generating all possible three letters keywords e.g. aaa, aab, aac.... zzy, zzz
below is my code:
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
keywords = []
for alpha1 in alphabets:
for alpha2 in alphabets:
for alpha3 in alphabets:
keywords.append[alpha1+alpha2+alpha3]
Can this functionality be achieved in a more sleek and efficient way?
agf
164k40 gold badges278 silver badges232 bronze badges
asked Aug 16, 2011 at 5:46
Aamir RindAamir Rind
37.5k21 gold badges121 silver badges158 bronze badges
keywords = itertools.product[alphabets, repeat = 3]
See the
documentation for itertools.product
. If you need a list of strings, just use
keywords = [''.join[i] for i in itertools.product[alphabets, repeat = 3]]
alphabets
also doesn't need to be a list, it can just be a string, for example:
from itertools import product
from string import ascii_lowercase
keywords = [''.join[i] for i in product[ascii_lowercase, repeat = 3]]
will work if you just want the lowercase ascii letters.
answered Aug 16, 2011 at 5:48
2
You could also use map instead of the list comprehension [this is one of the cases where map is still faster than the LC]
>>> from itertools import product
>>> from string import ascii_lowercase
>>> keywords = map[''.join, product[ascii_lowercase, repeat=3]]
This variation of the list comprehension is also faster than using ''.join
>>> keywords = [a+b+c for a,b,c in product[ascii_lowercase, repeat=3]]
answered Aug 16, 2011 at 6:08
John La RooyJohn La Rooy
285k51 gold badges358 silver badges498 bronze badges
2
from itertools import combinations_with_replacement
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for [a,b,c] in combinations_with_replacement[alphabets, 3]:
print a+b+c
agf
164k40 gold badges278 silver badges232 bronze badges
answered Aug 16, 2011 at 6:54
AsteriskAsterisk
3,5062 gold badges34 silver badges53 bronze badges
1
You can also do this without any external modules by doing simple calculation.
The PermutationIterator
is what you are searching for.
def permutation_atindex[_int, _set, length]:
"""
Return the permutation at index '_int' for itemgetter '_set'
with length 'length'.
"""
items = []
strLength = len[_set]
index = _int % strLength
items.append[_set[index]]
for n in xrange[1,length, 1]:
_int //= strLength
index = _int % strLength
items.append[_set[index]]
return items
class PermutationIterator:
"""
A class that can iterate over possible permuations
of the given 'iterable' and 'length' argument.
"""
def __init__[self, iterable, length]:
self.length = length
self.current = 0
self.max = len[iterable] ** length
self.iterable = iterable
def __iter__[self]:
return self
def __next__[self]:
if self.current >= self.max:
raise StopIteration
try:
return permutation_atindex[self.current, self.iterable, self.length]
finally:
self.current += 1
Give it an iterable object and an integer as the output-length.
from string import ascii_lowercase
for e in PermutationIterator[ascii_lowercase, 3]:
print "".join[e]
This will start from 'aaa' and end with 'zzz'.
agf
164k40 gold badges278 silver badges232 bronze badges
answered Aug 19, 2011 at 9:15
Niklas RNiklas R
15.6k25 gold badges94 silver badges197 bronze badges
chars = range[ord['a'], ord['z']+1];
print [chr[a] + chr[b] +chr[c] for a in chars for b in chars for c in chars]
answered Aug 16, 2011 at 7:32
We could solve this without the itertools by utilizing two function definitions:
def combos[alphas, k]:
l = len[alphas]
kRecur[alphas, "", l, k]
def KRecur[alphas, prfx, l, k]:
if k==0:
print[prfx]
else:
for i in range[l]:
newPrfx = prfx + alphas[i]
KRecur[alphas, newPrfx, l, k-1]
It's done using two functions to avoid resetting the length of the alphas, and the second function self-iterates itself until it reaches a k of 0 to return the k-mer for that i loop.
Adopted from a solution by Abhinav Ramana on Geeks4Geeks
answered Feb 28, 2019 at 6:21
1
Well, i came up with that solution while thinking about how to cover that topic:
import random
s = "aei"
b = []
lenght=len[s]
for _ in range[10]:
for _ in range[length]:
password = ["".join[random.sample[s,length]]]
if password not in b:
b.append["".join[password]]
print[b]
print[len[b]]
Please let me describe what is going on inside:
- Importing Random,
- creating a string with letters that we want to use
- creating an empty list that we will use to put our combinations in
- and now we are using range [I put 10 but for 3 digits it can be less]
- next using random.sample with a list and list length we are creating letter combinations and joining it.
- in next steps we are checking if in our b list we have that combination - if so, it is not added to the b list. If current combination is not on the list, we are adding it to it. [we are comparing final joined combination].
- the last step is to print list b with all combinations and print number of possible combinations. Maybe it is not clear and most efficient code but i think it works...
answered Jan 5 at 11:42
2
print[[a+b+c for a in alphabets for b in alphabets for c in alphabets if a !=b and b!=c and c!= a]]
This removes the repetition of characters in one string
a_r
4606 silver badges12 bronze badges
answered Jan 21, 2017 at 0:58