2014年8月26日 星期二

count_times

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
lst = list()
hour = list()
counts = dict()
timelst = list()
newlst = list()

for line in handle:
    if line.startswith('From'):
        words = line.split()
        if words[0] == 'From': # take out From, get rid of From: line
            lst.append(words[5]) # take time out of list
     
for hr in lst:
    timelst.append(hr[0:2]) # take hour out of time

for hour in timelst:
    counts[hour] = counts.get(hour, 0) + 1

for key, val in counts.items():
    newlst.append((key, val))

newlst.sort()

for val, key in newlst:
     print val, key

Output:
04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1


沒有留言:

張貼留言