Home > database >  Get all possible strings from list of letters in order
Get all possible strings from list of letters in order

Time:01-20

I am trying to make a python script that generates all the possible stings of lists of letters

These lists contain all the posssible letters that make up the string in order, like:

`
firstLetter = ["a", "s"]
secondLetter = ["e", "r"]
thirdLetter = ["w", "s"]`

I tried this:

`
import itertools

firstLetter = ["a", "s"]
secondLetter = ["e", "r"]
thirdLetter = ["w", "s"]

comfirst = list(itertools.combinations(range(firstLetter), 1))
combsecond = list(itertools.combinations(range(secondLetter), 1))
combthird = list(itertools.combinations(range(thirdLetter), 1))



comb = list(itertools.combinations(range(combfirst,combsecond,combthird), 3))

print(comb) `

Expected result:

aew arw sew srw aes ars ses srs

Actual result:

TypeError: 'list' object cannot be interpreted as an integer

CodePudding user response:

You can use itertools.product:

from itertools import product
list(map("".join, product(firstLetter, secondLetter, thirdLetter)))

This outputs:

['aew', 'aes', 'arw', 'ars', 'sew', 'ses', 'srw', 'srs']
  • Related