老是会报错keyerror,不知道为什么?

st="New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3"
list1=list(st.split(" "))
print(list1)
dict1=dict()
for i in list1:
if i in dict1:
dict1[i]=1
else:
dict1[i]+=1
print(sorted(dict1))

盲猜一下,这是要统计单词出现的频率嘛。
如果这个词不在最后的字典中,那么把这个词添加进去,并且这个词的次数置1,如果在,那么这个词出现的次数+1。
题主把判断的条件搞反了。(如有异议,我是垃圾)

st = "New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3"
list1 = list(st.split(" "))
print(list1)
dict1 = dict()
for i in list1:
    if i in dict1:
        dict1[i] += 1
    else:
        dict1[i] = 1
print dict1

if i in dect1.keys()
dect1是个字典,它不会直接包含1,你只能问它的key里有1还是value里有1

字典是通过key值访问的 dict['Age']