[Python] operator.itemgetter

Word Counter Sorted by Times(Chinese Version):

import operator
in_file = open("input.txt")
raw_article = in_file.read()
table={}
for word in raw_article:
 if word in table:
   table[word] = table[word]+1
 else:
   table[word] = 1
sorted_table = sorted(table.items(),key=operator.itemgetter(1),reverse=True)
for pair in sorted_table:
 print(pair[0],":",pair[1])

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *