Python: 字典应用题
Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
persons = list()
clist = list for line in handle:
line = line.rstrip()
clist = line.split()
#Guardian pattern
if line == '':
continue
if clist[0] is 'From':
persons.append(clist[1])
for person in persons:
counts[person] = counts.get(person,0) +1 bigcount = None
bigperson = None
for p,c in counts.items():
if bigcount is None or c>bigcount:
bigcount = c
bigperson = p print(bigperson, bigcount)
python 数据结构综合题:
- 每一个代码块都实现一个功能,功能之间不要冗杂:
- 以行为单位读文件
- .split() 把string拆成一个list, 如果list首元素为From,则把list[2]拿出来,存到新列表person list里面
- 用dictionary做histogram,把(key, value)是(人名,次数)
- 遍历 dictionary,找最大值
这样,功能分布清晰,提高代码可读性,也有利于debug
Guardian pattern:
防止line是空行,否则closet[0] 会报错: out of range. 因为是空list,没有list[0]元素
有个很好的debug教程:
Python data structures - week 4 - Assignment Chapter 8 - Worked exercise ( 看助教是怎么找错的~
Python: 字典应用题的更多相关文章
- Python字典和集合
Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- python字典中的元素类型
python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...
- python字典copy()方法
python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...
- python 字典实现类似c的switch case
#python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...
- python字典的常用操作方法
Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...
- Python 字典(Dictionary)操作详解
Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python 字典(Dictionary) setdefault()方法
描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...
随机推荐
- 最小二乘法的Java实现
最小二乘法原理十分简单,这里不再赘述.对于预测公式y' = a * x + b,最优解如下 double a = Sxy / Sxx; double b = yAvg - a * xAvg; doub ...
- MyDO
编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能提交测试代码和运行结果截图. od -tx -tc XXX是输出文件的十六进制数和ASCII字符或 ...
- 转:【专题八】P2P编程
引言: 前面的介绍专题中有朋友向我留言说介绍下关于P2P相关的内容的,首先本人对于C#网络编程也不是什么大牛,因为能力的关系,也只能把自己的一些学习过程和自己的一些学习过程中的理解和大家分享下的,下面 ...
- Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- 使用nosql实现页面静态化的一个小案列
页面静态化,其实就是将动态生成的php页面,变成静态的HTML页面,让用户直接访问.有一下几方面好处: 1,首先就是访问速度,不需要去访问数据库,或者缓存来获取哪些数据,浏览器直接加载渲染html页即 ...
- scrapy instantiation
start from scrapy.cmdline import execute execute(['scrapy', 'crawl', 'jokespider']) items.py import ...
- django ORM模型表的一对多、多对多关系、万能双下划线查询
一.外键使用 在 MySQL 中,如果使用InnoDB引擎,则支持外键约束.(另一种常用的MyIsam引擎不支持外键) 定义外键的语法为fieldname=models.ForeignKey(to_c ...
- Builgen 插件——IntelliJ IDEA和Eclipse Java Bean Builder模式代码生成器-比lombok更符合需求
builder模式在越来越多的项目中使用,类似于alibaba fastjson JSONObject.fluentPut(),调用一个方法后返回这个对象本身,特别适合构建一些参数超级多的对象,代码优 ...
- shell脚本一键安装redis
支持识别离线包和联网安装,自动修改使用后台运行模式,离线安装的方法是,将离线包和脚本放在同一个文件夹, 它会先识别有没有离线包, 有离线包就先安装离线包, 没有离线包就安装进行判断机器是否能联网, 能 ...
- Eclipse中手动清理项目缓存,
用过Eclipse或MyEclipse的小伙伴肯定遇到过这种情况: 代码出错后,在前台访问出问题.然后把代码改好,已经检查不到错误,可是项目在前台访问还是有问题. 这个时候,可能就是Eclipse/M ...