'TypeError: list indices must be integers [closed]
I'm trying to insert an item from one list to another and using an item from a list of numbers for the index but I'm getting this error even though I'm using integers for index numbers
and the other thing is that the same item is accepted as an index in the line just before the error
I even tried putting a number there just to test it but it gave me the same error
here's the code:
FoundLetters = ['p', 'l', '-', 'i', 'n']
MissingLetters = []
AllLetters = ['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']
def MissingCount():
for j in range(len(FoundLetters)):
if FoundLetters[j] == '-':
MissingLetters.append(int(j))
def iterate():
for i in range(len(AllLetters)):
for i in AllLetters:
FoundLetters.pop(MissingLetters[0])
FoundLetters.insert(MissingLetters[0], AllLetters[i])
MissingCount()
iterate()
and this was the exact error:
Traceback (most recent call last):
File "main.py", line 26, in <module>
iterate()
File "main.py", line 22, in iterate
FoundLetters.insert(MissingLetters[0], AllLetters[i])
TypeError: list indices must be integers or slices, not str
** Process exited - Return Code: 1 **
Press Enter to exit terminal
Solution 1:[1]
the problem with your code is that you are using i twice in the iterate function. So you are overwriting the first i. Therefore you are looping through AlLLetters and you try to index AllLeters with strings like "a". Try to change the second loop
Solution 2:[2]
I'd suggest using itertools.product
to generate all the possible replacement combinations:
import itertools
import string
found_letters = "pl-in"
missing_indices = [i for i, c in enumerate(found_letters) if c == "-"]
possible_words = (''.join(
r[i] if i in r else c
for i, c in enumerate(found_letters)
) for r in (
dict(zip(missing_indices, p))
for p in itertools.product(
string.ascii_lowercase,
repeat=len(missing_indices)
)
))
print(list(possible_words)) # ['plain', 'plbin', ...]
Using repeat
with the number of missing letters will allow this to work regardless of the number of missing letters. In the above code, p
is a product ('a'
, 'b'
, ...) and r
is the replacement map for a given product ({2: 'a'}
, {2: 'b'}
, ...). When there are multiple -
s in found_letters
, each p
will have that many letters ('aa'
, 'ab'
, ...) and r
will map the location of each -
to the letter that should replace it ({2: 'a', 4: 'a'}
, {2: 'a', 4: 'b'}
, ...).
Note that the sequence of possible_words
will get very large very quick if you have a lot of missing letters because it's exponential! Although it's convenient to turn it into a list
when you want to print the whole thing, you may want to leave it in generator form so you can filter it through the English dictionary and drop all the nonsense words as you see them.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Moritz Schaller |
Solution 2 |