我的程序在Python 2.0中工作,但我需要它在3.0或更高版本中工作.问题是新的Python不再具有该.has_key功能.我需要知道如何解决这个问题,以便它可以在新版本中使用.
dictionary = {}
for word in words:
if dictionary.has_key(word):
dictionary[word]+=1
else:
dictionary[word]=1
bonus = {}
for key in sorted(dictionary.iterkeys()):
print("%s: %s" % (key,dictionary[key]))
if len(key)>5: #if word is longer than 5 characters (6 or greater) save to list, where we will get top 10 most common
bonus[key]=dictionary[key]
Run Code Online (Sandbox Code Playgroud)