您的位置:首页 > 编程语言 > Python开发

python之 heapq -- show me the code 0006

2015-01-07 21:49 441 查看
题目要求统计出你每篇日记最重要的词。不太理解最重要的词是什么意思,所以把出现次数最多的单词弄出来了,

用到 heaqp 这种数据结构,heaqp 模块使用一个用对实现的优先级队列。 堆是一种简单的有序列表, 并且置入了堆的

相关规则。heapq模块有两个函数
nlargest(num,iteror,key) 和 nsmallest(num,iteror,key),方法返回可迭代对象中中最大(小)的

num个元素。参数key用于指定作为排序依据的键,默认为None。

<span style="font-size:14px;">nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print(heapq.nlargest(3, nums)) # Prints [42, 37, 23]
print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]</span>


以上是关于 heaqp 的示例代码, 以下是小题目的代码:

__author__ = 'Administrator'
# encoding=utf-8
import heapq
from collections import Counter

def mostly_important_word(file="youth.txt"):
"""第 0006 题:你有一个目录,放了你一个月的日记,都是 txt,
为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。"""

data = []
with open(file) as handle:
for line in handle:
words = line.split()
data.extend(words)
dictionary = dict(Counter(data))
arr = []
for key in dictionary:
temp = {"var": key, "count": dictionary[key]}
arr.append(temp)

word = heapq.nlargest(1, arr, key=lambda s: s["count"])
print("mostly important word is :", word[0]['var'])

if __name__ == '__main__':
mostly_important_word()


最近感觉到python很多细节上的东西真的很优雅,受到以前编程习惯的影响,很多思维上的定势真的一时难以扭转过来,
而且我相信,虽然代码很短,仍然有可优化的地方,只是我目前对 python 的了解程度还不够。 不管怎么说, 好好学习吧~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: