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()方法语法: ...
随机推荐
- python 读csv数据 通过改变分隔符去掉引号
import csv with open(r'C:\Temp\ff.csv') as f: f_csv=csv.reader(f,delimiter='\t') headers=next(f_csv) ...
- JAVA 中的 Collection 和 Map 以及相关派生类的概念
JAVA中Collection接口和Map接口的主要实现类 Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的 ...
- (Review cs231n) Gradient Calculation and Backward
---恢复内容开始--- 昨日之补充web. 求解下图的梯度的流动,反向更新参数的过程,表示为 输入与损失梯度的关系,借助链式法则,当前输入与损失之间的梯度关系为局部梯度乘以后一层的梯度. ---恢复 ...
- 51Nod 1433 0和5
小K手中有n张牌,每张牌上有一个一位数的数,这个字数不是0就是5.小K从这些牌在抽出任意张(不能抽0张),排成一行这样就组成了一个数.使得这个数尽可能大,而且可以被90整除. 注意: 1.这个数没有前 ...
- @RefreshScope 的作用
让在application.properties里自定义的变量也能通过@Value 注解正常注入
- linux下的ifconfig命令
ifconfig工具不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置. 1.命令格式: ifconfig [网络设备] [参数] 2.命令功能: ifconfig 命令用来查看和配置网络设 ...
- Tomcat启动报错:[Failed to start component]的解决方案
在MyEclipse中启动Tomcat,该Tomcat仅部署了一个报错项目,启动Tomcat Server的全部信息如下: usage: java org.apache.catalina.startu ...
- MyEclipse中项目运行时发生了Tomcat报错:[java.lang.OutOfMemoryError: PermGen space]
Tomcat内存溢出,异常信息如下: 十一月 26, 2017 1:52:26 下午 org.apache.catalina.core.ContainerBase$ContainerBackgroun ...
- MySQL5.7 的新特点
1.安全性 MySQL 5.7 的目标是成为发布以来最安全的 MySQL 服务器,其在 SSL/TLS 和全面安全开发方面有一些重要的改变. mysql.user表结构升级 MySQL5.7用户表my ...
- Vue小案例 之 商品管理------批量删除与商品数量的调整
通过索引进行删除,进行测试,是否获取其索引: 测试效果: 测试代码,在vue中定义一个空的数组,以便后面进行数据的绑定: data:{ imgUrl:'../res/images/', imgName ...