How can I generate a list of possible 'words' when all I know is the number of letters of each word in Python?
Question:
I know this is probably a dumb question and I am missing something really simple, but I can’t for the life of me figure out where I am going wrong… I am ultimately trying to generate a list of all possible letter combinations with a string of a given length. I know some of these letter combinations wont be pronounceable, but I am going to call them ‘words’ nonetheless for simplicity. For example, if I know my string is one letter long, I want to generate a list of letters, "A" though "Z", 26 elements long. If I know my string is two letters long, I will need a list of "words" from "AA" to "ZZ", 676 elements long. The rub comes where I do not know the number of letters ahead of time.
If I know my ‘word’ will only be one letter, then I can do this:
letters = ["A", "B", "C", ... "X", "Y", "Z"]
words = []
for x in range(26):
words.append(letters[x])
print(words)
If I know my ‘word’ will be two letters long, I can do this:
letters = ["A", "B", "C", ... "X", "Y", "Z"]
words = []
for x in range(26):
for y in range(26):
words.append(letters[x] + letters[y])
print(words)
What I can’t for the life of me figure out is how to do this when I don’t know ahead of time how many letters my word will have. I basically want a formula where I give it the number of letters, and it gives me a list of all the possible words, that number of letters long. Something to the effect of:
def possible_words(word_length):
list_of_words = []
number_of_words = 26 ** word_length
for x in range(number_of_words):
'''generate the new word here'''
list_of_words.append(new_word)
return list_of_words
Where am I going wrong? What am I missing? I feel like I am so close, I know this has to be a simple problem, but I have been racking my brain for a few hours now and can’t figure it out. I feel like the solution lies in something involving recursion? And I think I maybe can’t get the numbers to line up with the words because of a combination of zero-indexing and the fact that a ‘zero letter’ doesn’t exist? I don’t know, the more I think the more confused I get… Please help! Thanks in advance!
EDIT: Thanks to those who responded and specifically to njp and Thierry Lathuille who directed me towards itertools.product! The solutions was even more pythonic than I could have hoped for! Below is the code I ended up using, in case anyone else comes across the same issue:
import itertools
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
word_length = 2 # The 2 is just for testing, actual value will be determined programatically...
letter_tuples = itertools.product(alphabet, repeat=word_length)
words = ["".join(letter_tuple) for letter_tuple in letter_tuples]
print(words)
Answers:
this is a standard permutations problem.
what you want is:
itertools.permutations
You can get what you want using itertools.product
, specifically:
import itertools as it
list(it.product(letters, repeat=3))
('A', 'A', 'B'),
('A', 'A', 'C'),
('A', 'A', 'D'),
('A', 'A', 'E'),
('A', 'A', 'F'),
...
it.product
works exactly as nested for-loops.
I created a bit of code. I think this is what you mean. You change the variable on how many times each letter is shown. I hope this helps.
word_length = 1
list_of_words = []
letters = ["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"]
letter_on = 0
count = 0
for x in range(word_length*len(letters)):
if letter_on != 27:
if count != word_length:
list_of_words.append(letters[letter_on])
count+=1
else:
count=0
letter_on+=1
else:
break
print(list_of_words)
I know this is probably a dumb question and I am missing something really simple, but I can’t for the life of me figure out where I am going wrong… I am ultimately trying to generate a list of all possible letter combinations with a string of a given length. I know some of these letter combinations wont be pronounceable, but I am going to call them ‘words’ nonetheless for simplicity. For example, if I know my string is one letter long, I want to generate a list of letters, "A" though "Z", 26 elements long. If I know my string is two letters long, I will need a list of "words" from "AA" to "ZZ", 676 elements long. The rub comes where I do not know the number of letters ahead of time.
If I know my ‘word’ will only be one letter, then I can do this:
letters = ["A", "B", "C", ... "X", "Y", "Z"]
words = []
for x in range(26):
words.append(letters[x])
print(words)
If I know my ‘word’ will be two letters long, I can do this:
letters = ["A", "B", "C", ... "X", "Y", "Z"]
words = []
for x in range(26):
for y in range(26):
words.append(letters[x] + letters[y])
print(words)
What I can’t for the life of me figure out is how to do this when I don’t know ahead of time how many letters my word will have. I basically want a formula where I give it the number of letters, and it gives me a list of all the possible words, that number of letters long. Something to the effect of:
def possible_words(word_length):
list_of_words = []
number_of_words = 26 ** word_length
for x in range(number_of_words):
'''generate the new word here'''
list_of_words.append(new_word)
return list_of_words
Where am I going wrong? What am I missing? I feel like I am so close, I know this has to be a simple problem, but I have been racking my brain for a few hours now and can’t figure it out. I feel like the solution lies in something involving recursion? And I think I maybe can’t get the numbers to line up with the words because of a combination of zero-indexing and the fact that a ‘zero letter’ doesn’t exist? I don’t know, the more I think the more confused I get… Please help! Thanks in advance!
EDIT: Thanks to those who responded and specifically to njp and Thierry Lathuille who directed me towards itertools.product! The solutions was even more pythonic than I could have hoped for! Below is the code I ended up using, in case anyone else comes across the same issue:
import itertools
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
word_length = 2 # The 2 is just for testing, actual value will be determined programatically...
letter_tuples = itertools.product(alphabet, repeat=word_length)
words = ["".join(letter_tuple) for letter_tuple in letter_tuples]
print(words)
this is a standard permutations problem.
what you want is:
itertools.permutations
You can get what you want using itertools.product
, specifically:
import itertools as it
list(it.product(letters, repeat=3))
('A', 'A', 'B'),
('A', 'A', 'C'),
('A', 'A', 'D'),
('A', 'A', 'E'),
('A', 'A', 'F'),
...
it.product
works exactly as nested for-loops.
I created a bit of code. I think this is what you mean. You change the variable on how many times each letter is shown. I hope this helps.
word_length = 1
list_of_words = []
letters = ["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"]
letter_on = 0
count = 0
for x in range(word_length*len(letters)):
if letter_on != 27:
if count != word_length:
list_of_words.append(letters[letter_on])
count+=1
else:
count=0
letter_on+=1
else:
break
print(list_of_words)