#!/usr/bin/env python #Filename:readfile.py codelist=[] f=file('input.txt','r') lines = f.readlines(); for i in range(len(lines)): line=lines[i].strip('\n'); print line codelist+=line print codelist ########################################### map={} for i in codelist: if map.has_key(i)==False: map.setdefault(i,1) else: count=map.get(i)+1 map[i]=count print map ########################################### # sort map # # sort map # ########################################### map=sorted(map.iteritems(),key=lambda asd:asd[1],reverse=True) print map list_map=list(map); fo=file('output.txt','w') for i in list_map: i_list = list(i) fo.write(str(i_list[0])) print str(i_list[0]) fo.write("\t \t \t \t") fo.write(str(i_list[1])) print "\n" print str(i_list[1]) fo.write("\n") f.close() fo.close()
原文地址:http://blog.csdn.net/chujiangke001/article/details/41599237