def calculate(s): list = s.split() dict = {} for word in list: if word not in list: dict[word] = 1 else: dict[word] += 1 return dict
if name == 'main': words = "I Like python and you Like java too do you Like python or java" print calculate(words)
报错:Traceback (most recent call last):
File "D:/KDR/gg.py", line 15, in <module>
print calculate(words)
File "D:/KDR/gg.py", line 10, in calculate
dict[word] += 1
KeyError: 'I'
妈的居然报键值索引错误 2333
1
whoami9894 2018-04-26 17:17:56 +08:00 via Android
if 语句恒为 false
应该是 if word not in dict.keys() |
2
U87 OP @whoami9894 谢谢我 sb 了
|