2020-11-26 Coursera-PY4E-Execrise10.2

10.2 Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
hours = dict()
result = list()
for line in handle:
    line.rstrip()
    if line.startswith("From"):
        if not line.startswith('From:'):
            email = line.split()
            time = email[5]
            hour = time.split(":")[0]
            hours[hour] = hours.get(hour,0)+1
#这里注意hours.items输出的是包含元组的列表,元组无法排序
for k,v in hours.items():
    newtup = (k,v)
    result.append(newtup)
result.sort()
for k,v in result:
    print(k,v)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 走着走着已经到了玩不起的年龄,走着走着我醒了不再对所有人好,谁对我好我就对谁好,走着走着我知道,日子不一定生情
    乌鸦fishnoon阅读 179评论 0 1
  • 时间的控制 有点难 人性的控制 有点难 还是没有健身 第一个月 没有收入。 还要去合肥 宿州 徐州 晚安 世界。
    琛周阅读 217评论 0 0
  • 努力工作_1542阅读 183评论 0 0
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,870评论 16 22
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    余生动听阅读 10,920评论 0 11

友情链接更多精彩内容