[英]'TypeError: list indices must be integers, not str' Python 3
I need a bit of help with this assignment (first time posting on SE so please excuse my lack of posting etiquette if any) 我需要有关此作业的一些帮助(第一次在SE上发帖,所以请原谅我缺乏发帖礼节的人)
So for this code I had to write a spell checker. 因此,对于此代码,我不得不编写一个拼写检查器。 Basically what it is supposed to do is:
基本上应该做的是:
1.) Check through two lists (One is a dictionary, where we get all the correctly spelled words, the other is the user input list which should have an incorrectly spelled word or two) 1.)检查两个列表(一个是字典,其中我们得到所有正确拼写的单词,另一个是用户输入列表,其中应该包含一两个拼写错误的单词)
2.) suggest a correct word in place of the misspelled word (example would be if I spelled heloo, the spell checker would say i spelled that wrong and would suggest the word is hello, help, etc.) 2.)建议使用正确的单词代替拼写错误的单词(例如,如果我拼写了heloo,拼写检查器会说我拼错了,并提示单词是hello,help等。)
My biggest problem right now is at line 19, I am getting the list indices must be integers problem. 我现在最大的问题是在第19行,我得到的列表索引必须是整数问题。
Any help is appreciated, and help with finishing this would also be much appreciated! 感谢您提供任何帮助,也非常感谢您完成此工作! I feel like outside of the syntax more could be improved upon.
我觉得在语法之外,还有更多可以改进的地方。 Thanks.
谢谢。
here is the code, it is not completely finished 这是代码,还没有完全完成
import re
def words_from_file(filename):
try:
f = open(filename, "r")
words = re.split(r"[,.;:?\s]+", f.read())
f.close()
return [word for word in words if word]
except IOError:
print("Error opening %s for reading. Quitting" % (filename))
exit()
user_word = words_from_file('user_word.txt')
suggestion = words_from_file('big_word_list.txt')
sug_list = []
for a in user_word:
if user_word[a] not in suggestion:
print ("Spelling error: %s"%(user_word[a]))
for i in suggestion:
for j in suggestion[i]:
if len(suggestion[i][j]) == len(user_word[a]-2):
count = 0
similarities = len(user_word[a])
for k in suggestion[i][j]:
if suggestion[i][j][k] in suggestion:
count+=1
if count >= similarities:
sug_list.append(suggestion[i][j])
Change: 更改:
for a in user_word:
if user_word[a] not in suggestion:
Into: 成:
for a in user_word:
if a not in suggestion:
because all items in user_word
list will be iterated using the a
variable. 因为
user_word
列表中的所有项目都将使用a
变量进行迭代。 The a
will in each iteration contain a nonempty string obtained from the line split. 每次迭代中的
a
will包含从行拆分中获得的非空字符串。 You can only use numerical index with a list type. 您只能将数字索引与列表类型一起使用。 Originally you've used a string in place of numeric index which causes the error message.
最初,您使用字符串代替数字索引,这会导致错误消息。
List can slice using integers
not str
列表可以使用
integers
而不是str
切片
Change 更改
if user_word[a] not in suggestion: #to==> if a not in suggestion:
for j in suggestion[i]: #to==> for j in i
for k in suggestion[i][j]: #to==> for k in j
Also many errors slice errors
like suggestion[i][j][k]
etc 也有很多错误会切片
errors
例如suggestion[i][j][k]
等
So generally 所以一般
for i in [1,2,3]:
print(i) # gives 1,2,3.No need of [1,2,3][i]
Also, you can use range(len)
另外,您可以使用
range(len)
for a in range(len(user_word)):
if user_word[a] not in suggestion:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.