[英]Python: sort dict by values, print key and value
我正在嘗試對文件中的所有單詞進行排序,並返回引用的前20個單詞。 這是我的代碼:
import sys
filename = sys.argv[2]
def helper_function(filename):
the_file = open(filename, 'r')
words_count = {}
lines_in_file = the_file.readlines()
for line in lines_in_file:
words_list = line.split()
for word in words_list:
if word in words_count:
words_count[word.lower()] += 1
else:
words_count[word.lower()] = 1
return words_count
def print_words(filename):
words_count = helper_function(filename)
for w in sorted(words_count.keys()): print w, words_count[w]
def print_top(filename):
words_count = helper_function(filename)
for w in sorted(words_count.values()): print w
def main():
if len(sys.argv) != 3:
print 'usage: ./wordcount.py {--count | --topcount} file'
sys.exit(1)
option = sys.argv[1]
filename = sys.argv[2]
if option == '--count':
print_words(filename)
elif option == '--topcount':
print_top(filename)
else:
print 'unknown option: ' + option
sys.exit(1)
if __name__ == '__main__':
main()
我定義print_top()的方式返回的是words_count字典的排序值,但我想這樣打印:Word:Count
您的建議非常有價值!
您很親近,只需根據值對dict項目進行排序(這就是itemgetter所做的事情)。
>>> word_count = {'The' : 2, 'quick' : 8, 'brown' : 4, 'fox' : 1 }
>>> from operator import itemgetter
>>> for word, count in reversed(sorted(word_count.iteritems(), key=itemgetter(1))):
... print word, count
...
quick 8
brown 4
The 2
fox 1
對於“前20名”,我建議您看一下heapq
>>> import heapq
>>> heapq.nlargest(3, word_count.iteritems(), itemgetter(1))
[('quick', 8), ('brown', 4), ('The', 2)]
要以“鍵:值”的形式獲取輸出,在字典中填充了值和鍵之后,請使用函數的返回值,如下所示:
def getAllKeyValuePairs():
for key in sorted(dict_name):
return key + ": "+ str(dict_name[key])
或特定的鍵值對:
def getTheKeyValuePair(key):
if (key in dict_name.keys()):
return key + ": "+ str(dict_name[key])
else:
return "No such key (" + key + ") in the dictionary"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.