Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get all possible strings from list of letters in order

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

`
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

>Solution :

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading