我正在编写一个程序来尝试计算列表中最重复的单词出现次数。我不断收到一个错误,说:索引错误。尽管当我打印word_list的列表时,它显示有108个元素。有人能指出我的方向是错误的吗?Python上的列表索引错误
length = len(word_list)
num = 0
print(length)
while num <= length:
ele = word_list[num]
if ele in wordDict:
wordDict[ele] = wordDict[ele] +1
repeat = repeat + 1
if repeat > highestRepeat:
highestRepeat = repeat
else:
wordDict[ele] = 1
repeat = 1
num = num+1
你在哪里定义重复?我认为你应该使用你'if wordDict [ele]> highestRepeat' – kame
在'while num <= length:'change'<='到'<' –