How do you get all the possible combinations of letters in python?

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:

  1. Importing Random,
  2. creating a string with letters that we want to use
  3. creating an empty list that we will use to put our combinations in
  4. and now we are using range [I put 10 but for 3 digits it can be less]
  5. next using random.sample with a list and list length we are creating letter combinations and joining it.
  6. 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].
  7. 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

How do I get all the combinations of a Python letter?

“Python all combinations of letters” Code Answer's.
test_str = "abc".
res = [test_str[i: j] for i in range[len[test_str]].
for j in range[i + 1, len[test_str] + 1]].
print[res]#['a', 'ab', 'abc', 'b', 'bc', 'c'].

How can I create all possible combination of words from characters of given word in Python?

How can I create all possible combination of words from characters of given word in Python?.
Code s='ABC' print list[itertools.permutations[s,2]].
Output [['A','B'],['B','A'],['B','C'],['C','B'],['A','C'],['C','A']] ... .
Code s='ABC' print list[itertools.combinations[s,2]].
Output [['A','B'],['B','C'],['A','C']].

How do you get all possible combinations of a set of numbers in Python?

Powerset—How to Get All Combinations of a List in Python.
Import the built-in itertools module..
Specify a list of items..
Initialize an empty list for storing the combinations..
Create a loop that loops values from 0 to the length of the list + 1..

How many combinations of letters are possible?

This gives that the number of combinations that are possible with the alphabet, or 26 letters, without repetition is 67,108,863.

Chủ Đề