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()方法语法: ...
随机推荐
- printf、fprintf、sprintf和snprintf 区别
都是把格式好的字符串输出,只是输出的目标不一样: 1 printf,是把格式字符串输出到标准输出(一般是屏幕,可以重定向). 2 sprintf,是把格式字符串输出到指定字符串中,所以参数比print ...
- PS火焰文字制作
火焰文字制作: 最终效果 第一步: 新建图层,并输入文字(这里不做详细解说)
- 微信h5支付“网站域名ICP备案主体与商户号主体不一致”的解决方法,H5微信支付 授权函下载
如下图所示: 微信h5支付“网站域名ICP备案主体与商户号主体不一致”: 需提交H5微信支付 授权函 下载地址:https://download.csdn.net/download/a72400815 ...
- Django设计模式
单例模式: 建造者模式: 示例: from enum import Enum import time PizzaProgress = Enum('PizzaProgress', 'queued pre ...
- RGB颜色对照表
RGB颜色对照表 https://www.cnblogs.com/android100/p/android-rgb-list.html #FFFFFF #FFFFF0 #FFFFE0 ...
- 使用ccache大幅度加速gcc编译速度至少1倍以上(不需要修改任何编译选项)
因为我们整个项目都是使用c++开发的,生成的so足有50M,原来编译一遍要三五分钟,一个针对oracle,一个针对mysql,整个轮回下来这部分就要10来分钟,加上代码上传.翻译,一轮配管打包下来二三 ...
- mvc 文件下载
public class DownLoadHelper { /// <summary> /// WriteFile实现下载--测试通过 /// </summary> /// & ...
- opencv学习之路(11)、图像几何变换
一.图像缩放 #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat src=imread("E:// ...
- 牛客练习赛24题解(搜索,DP)
A题,C题不讲,基础题(但是我要抨击一下这次比赛,卡cin,cout,卡的太狠了,根本就不让过的那种,QAQ) 链接:https://www.nowcoder.com/acm/contest/157/ ...
- 动态规划之97 Interleaving String
题目链接:https://leetcode-cn.com/problems/interleaving-string/description/ 参考链接:https://blog.csdn.net/u0 ...