对字典按照value排序,并取值最大的前3个

dicts={"AGCT":25,"TTAA":1,"TTCA":2,"CCCC":3}
outfile=file("ceshi","w")
#print [k for k in sorted(dicts.values())][0:2]
dict2=sorted(dicts.items(),key=lambda item:item[1],reverse=True)[0:2]
for key,value in dict2:
    outfile.write("%s\t%d\n" % (key,value))

你可能感兴趣的:(对字典按照value排序,并取值最大的前3个)