-
Notifications
You must be signed in to change notification settings - Fork 10
/
program.py
21 lines (17 loc) 路 961 Bytes
/
program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
name = raw_input("Enter file:")
if len(name) < 1 : name = "text.txt"
handle = open(name)
hcount = dict() #create empty dictionary
hlst = [] #create empty list
for line in handle:
words = line.split()
if len(words) > 2 and words[0] == 'From': #Select lines with 'From'
hr = words[5].split(':') #Select hour (5th index) and split string with colon
hcount[hr[0]] = hcount.get(hr[0], 0) + 1 #increase count for each hour
else:
continue
for k,v in hcount.items(): #k = hour, v = count
hlst.append((k,v)) #append tuples to list
hlst.sort() #sort list by hour
for k,v in hlst: #loop through list of tuples
print k,v #print counts sorted by hour