python报错

1、

1
2
3
4
5
6
7
8
9
10
11
12
word_freq = {}
for word in words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
for i in range(word_freq):
print(word)
print(word_freq[word])

TypeError: 'dict' object cannot be interpreted as an integer
类型错误:'dict'对象不能被解释为整数
在打印字典的键值对时出现上述错误,首先是word没有参与循环,其次range(word_freq)使用错误
1
2
3
4
5
6
7
8
word_freq = {}
for word in words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
for word, freq in word_freq.items():
print(word,word_freq[word])


python报错
http://jrhu0048.github.io/2024/03/07/python/python-bao-cuo-ji-he/
作者
JR.HU
发布于
2024年3月7日
更新于
2024年7月18日
许可协议