python简单应用:字数统计
来源:互联网 发布:淘宝旺旺卖家版2017 编辑:程序博客网 时间:2024/05/16 00:47
每次写代码,LZ感觉还是得自己手敲一遍。。。一遍?一遍哪够?看着别人现成的代码觉得自己都能看的懂,等到自己上手解决自己任务的时候,才发现根本不是这么一回事。看了很多大神的博客,基本上都有一颗强大的内心,为啥?码了那么久的代码,结果一个bug就可能前功尽弃,从头再来,也是只能默默鼓励下自己:每天进步一点点!
"""Count words."""def count_words(s, n): """Return the n most frequently occuring words in s.""" # TODO: Count the number of occurences of each word in s import re listOfTokens = re.split(r'\W*', s) # print (listOfTokens) ss = set(listOfTokens) # print (ss) bow = [0] * len(ss) ll = list(ss) for word in listOfTokens: if word in ss: bow[ll.index(word)] += 1 dd = {} for i in range(len(ss)): dd[ll[i]] = bow[i] # sort_dd = sorted(dd.items(), key = lambda asd:asd[1], reverse = True) sort_dd = sorted(dd.items(), key = lambda asd:(-asd[1], asd[0])) top_ll = dict(sort_dd[0:n]) top_n = top_ll.keys() # TODO: Return the top n most frequent words. return top_ndef test_run(): """Test count_words() with some inputs.""" print (count_words("cat bat mat cat bat cat", 3)) print (count_words("betty bought a bit of butter but the butter was bitter", 3))if __name__ == '__main__': test_run()test_run()
代码量不大,但是对于python入门的新手任务来说还是有挑战的O(∩_∩)O
0 0
- python简单应用:字数统计
- Python 简单应用--文章字数统计
- 字数统计-简单哈希表
- python 统计excel 字数
- python统计文本字数
- 用 Python 统计字数
- python统计文本文件的字数
- python之西游记字数统计
- 用 Python 统计高频字数
- Spark应用——进行字数统计
- Java简单统计文本字数方法
- 字数统计
- 字数统计
- 字数统计
- VBA 字数统计
- 统计文本字数
- 统计页面字数
- JS统计字数
- win10下Anaconda管理Python开发环境
- 搭建wamp环境,数据库基础知识
- seekg()与tellg()相关文件操作
- 189. Rotate Array
- bzoj 4445: [Scoi2015]小凸想跑步 (半平面交)
- python简单应用:字数统计
- linux arp代理,DMZ与NAT
- 随机过程基本概念
- android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索
- NandFlash驱动超详细分析
- 淘宝网分布式存储技术视频
- 2016-2017学年第二学期C++第五章(2)
- NSString的内存管理
- Xargs用法详解