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()方法语法: ...
随机推荐
- centos 安装MySQL全过程
1.到chinaunix下载mysql 下载地址: http://download.chinaunix.net/download/0008000/7159.shtml 2.上传到CentOS服务器 本 ...
- 安装PG3.0详细教程附图
从公司要求开始着手调研PG到今天上午都还不知道如何安装PG.. 囧的离谱.. 看了半天的PG官网 就这个网页我瞅了半天..对你没看错 半天 少说有10分钟..原谅我的英文不是非常好..但是我知道什么意 ...
- 安装ES6及HEAD插件
1.下载相应npm包 es6地址:https://www.elastic.co/downloads/elasticsearch head插件地址:https://github.com/mobz/ela ...
- excel vba 数据分析
(Visual Basic Application) VBA(Visual Basic for Application)是Microsoft Office系列软件的内置编程语言,其语法结构与Visua ...
- nvidia-docker
参考资料 https://github.com/NVIDIA/nvidia-docker/wiki/Installation#prerequisites https://github.com/NVID ...
- Com类型
/* VARIANT STRUCTURE * * VARTYPE vt; * WORD wReserved1; * WORD wReserved2; * WORD wReserved3; * unio ...
- jquery easyui datagrid 空白条处理 自适应宽高 格式化函数formmater 初始化时会报错 cannot read property 'width'||'length' of null|undefined
1---表格定义好之后右侧可能会有一个空白条 这个空白条是留给滚动条的,当表格中的一页的数据在页面中不能全显示时会自动出现滚动条,网上有很多事要改源码才可以修改这个,但是当项目中多处用到时,有的需要滚 ...
- The Little Prince-12/10
The Little Prince-12/10 审判自己比审判别人难多了.如果你成功地正确审判了自己,那么你就是一个真正的智者了. ————确实,正视自己是非常难的人生准则.以人为镜,可以明得失,从别 ...
- The Little Prince-11/29
The Little Prince-11/29 The wheat fields have nothing to say to me. And that is sad. But you have ha ...
- Kattis之旅——Factovisors
The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...