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 数据结构综合题:

  1. 每一个代码块都实现一个功能,功能之间不要冗杂:
  2. 以行为单位读文件
  3. .split() 把string拆成一个list, 如果list首元素为From,则把list[2]拿出来,存到新列表person list里面
  4. 用dictionary做histogram,把(key, value)是(人名,次数)
  5. 遍历 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: 字典应用题的更多相关文章

  1. Python字典和集合

    Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...

  2. python 字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  3. python字典中的元素类型

    python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...

  4. python字典copy()方法

    python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...

  5. python 字典实现类似c的switch case

    #python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...

  6. python字典的常用操作方法

    Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...

  7. Python 字典(Dictionary)操作详解

    Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...

  8. Python 字典(Dictionary) get()方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  9. Python 字典(Dictionary) setdefault()方法

    描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...

随机推荐

  1. 【Alpha版本】冲刺阶段——Day5

    [Alpha版本]冲刺阶段--Day5 阅读目录 今日进展 问题困难 明日任务 今日贡献量 站立式会议 TODOlist [今日进展] 完成登录类代码 public void LOGIN() { co ...

  2. eclipse 依赖别的 工程 断点进不去

    maven Debug 发现进不了断点. 点击右键-->Properties-->Java Compiler-->Classfile Generation, 勾选上Add line  ...

  3. GUI常用对话框3

    %进度条 %waitbar h=waitbar(,'实例'); get(h); %获得进度条的子对象 get(get(h,'Children')) ha=get(h,'Children'); %获得坐 ...

  4. GUI常用对象的属性

    %常用对象的属性 %.figure %hf=figure; %get(hf); %改变颜色 set Color %set(hf,'Color','w'); %去掉默认的菜单 Menubar %set( ...

  5. [转载]WebService使用的一些总结

    什么是WebService: 这个不用我在这里废话,网上的资料一搜一大把,如果你没有接触过这方面的知识,你可以先去网上查一下.这里我只想说一下我印象比较深刻的几点: WebService是基于soap ...

  6. win10 +python3.6环境下安装opencv以及pycharm导入cv2有问题的解决办法

    一.安装opencv 借鉴的这篇博客已经写得很清楚了--------https://blog.csdn.net/u011321546/article/details/79499598       ,这 ...

  7. foreach嵌套遍历循环的问题

    在foreach嵌套循环中使用==和equals的问题 JSONArray ja1= new JSONArray(); JSONArray ja2 = new JSONArray(); JSONObj ...

  8. ATM开学测试(未完成)

    package ATM_design; //信1705-2 20173456 张浩 import java.util.*; import java.io.File; import java.io.Fi ...

  9. ELK日志分析系统

    部署环境 192.168.1.147 kibana.logstash.Escluster-node-1 192.168.1.151 filebeat.Escluster-node-2.nginx 软件 ...

  10. Docker学习笔记之Docker应用于服务化开发

    0x00 概述 上一节里我们谈到了小型的独立项目如何使用 Docker Compose 来搭建程序的运行环境,对于由多人或多部门参与的中大型服务化架构的项目,仅由一个 Docker Compose 项 ...