【发布时间】:2020-12-02 06:33:37
【问题描述】:
book_title = ['great', 'expectations','the', 'adventures', 'of', 'sherlock','holmes','the','great','gasby','hamlet','adventures','of','huckleberry','fin']
word_counter = []
for words in book_title:
if words not in word_counter:
word_counter[words]=1
else:
word_counter[words]+=1
我遇到了需要更正的错误类型,我应该做些什么来改进这一点
TypeError: 列表索引必须是整数,而不是 str
【问题讨论】:
-
你需要创建字典而不是列表来保持字数统计
-
你可能需要一个字典。
word_counter = {}。 -
或者只是:
word_counter = Counter(book_title) -
请提供完整的追溯
标签: python python-3.x list