Hello,
I have a programming assignment to count number of occurrences of hours in particular file. Below is the code:
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
largest = None
fh = open(fname)
counts = dict()
test = list()
for line in fh:
line = line.rstrip()
if not line.startswith('From'): continue
if line.startswith('From:'): continue
words = line.split()
h = words[5]
h = h.split(':')
test.append(h[0])
for w in tes:
counts[w] = counts.get(w, 0 ) + 1
print test
print counts
Output of "print test" which I believe is ok:
['09', '18', '16', '15', '15', '14', '11', '11', '11', '11', '11', '11', '10', '10', '10', '09', '07', '06', '04', '04', '04', '19', '17', '17', '16', '16', '16']
Output of "print counts":
{'11': 111, '10': 42, '15': 47, '14': 22, '04': 24, '16': 31, '19': 6, '18': 26, '09': 39, '17': 9, '06': 10, '07': 11}
I don't know why "print counts" is giving a large number of counts?
---------- Post updated at 11:52 AM ---------- Previous update was at 09:53 AM ----------
I figured out the issue!
It was the nested FOR loop:
for w in tes: counts[w] = counts.get(w, 0 ) + 1
I have written the code again:
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
largest = None
fh = open(fname)
counts = dict()
test = list()
lst = list()
for line in fh:
line = line.rstrip()
if not line.startswith('From'): continue
if line.startswith('From:'): continue
words = line.split()
h = words[5]
h = h.split(':')
test.append(h[0])
for w in test:
counts[w] = counts.get(w, 0 ) + 1
print test
print counts
Thanks!