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。
以上是关于 heaqp 的示例代码, 以下是小题目的代码:
最近感觉到python很多细节上的东西真的很优雅,受到以前编程习惯的影响,很多思维上的定势真的一时难以扭转过来,
而且我相信,虽然代码很短,仍然有可优化的地方,只是我目前对 python 的了解程度还不够。 不管怎么说, 好好学习吧~
用到 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 的了解程度还不够。 不管怎么说, 好好学习吧~
相关文章推荐
- python show-me-the-code 第0004
- Python Show-Me-the-Code 第 0004 题 统计单词
- Python Show-Me-the-Code 第 0017,0018,0019 题 写XML文件
- Show me the code之Python练习册 Q11~12 关键词过滤
- python show-me-the-code 第0010题
- python 之 Counter ---show me the code 0004
- python PIL --show me the code 0000
- Python Show-Me-the-Code 第 0022 题 批量图片处理2
- Python Show-Me-the-Code 第 0014,0015,0016 题 写Excel文件
- Show me the code之Python练习册 Q10 生成验证码
- Python Show-Me-the-Code 第 0000 题 图片右上角加数字
- Python Show-Me-the-Code 第 0010 题 生成验证码图片
- Python Show-Me-the-Code 第 0006 题 最重要的词
- Python Show-Me-the-Code 第 0023 题 Web留言簿应用
- Python Show-Me-the-Code 第 0002题
- Python Show-Me-the-Code 第 0001 题 生成激活码
- Python Show-Me-the-Code 第 0008 题 提取HTML正文内容
- Python Show-Me-the-Code 第 0009 题 提取网页中的超链接
- github/python/ show me the code 25题(一)
- Show me the code之Python练习册 Q4~7