Skip to content

Instantly share code, notes, and snippets.

@zcakzwa
Created July 5, 2016 12:43
Show Gist options
  • Save zcakzwa/2bf34f782db0c43847250b5fa86bc2bf to your computer and use it in GitHub Desktop.
Save zcakzwa/2bf34f782db0c43847250b5fa86bc2bf to your computer and use it in GitHub Desktop.
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 [email protected] Sat Jan 5 09:14:16 2008 Once you have accumulated the c…
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
d=dict()
for line in handle:
if not line.startswith("From "):
continue
else:
line=line.split()
line=line[5]
line=line[0:2]
d[line]=d.get(line,0)+1
lst=list()
for value,count in d.items():
lst.append((value,count))
lst.sort()
for value,count in lst:
print value,count
@amber020323
Copy link

name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)

for line in handle:
if not line.startswith('From'):
continue
else:
line = line.split()
word = line[5]
word = word.split(':')
word = word[0]
counts = dict()
for count in word:
counts[count] = counts.get( count, 0) +1
lst = list()
for k,v in counts.items():
lst.append(v,k)
lst = sorted(lst, reverse=True)

for v,k in lst:
print(k,v)

My code can not do well, can anyone find some problems? thankyou!!!

@theoriginalkamdia
Copy link

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
d=dict()
for line in handle:
if not line.startswith("From "):
continue
else:
line=line.split()
line=line[5]
line=line[0:2]
d[line]=d.get(line,0)+1

lst=list()
for value,count in d.items():
lst.append((value,count))

lst.sort()
for value,count in lst:
print(value,count)

@maulya230
Copy link

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
d=dict()
for line in handle:
if not line.startswith("From "):
continue
else:
line=line.split()
line=line[5]
line=line[0:2]
d[line]=d.get(line,0)+1

lst=list()
for value,count in d.items():
lst.append((value,count))

lst.sort()
for value,count in lst:
print (value,count)

@Bonllu
Copy link

Bonllu commented Jan 12, 2024

Hi, here's mine

Captura de pantalla horario mail

@rodrigo-calle
Copy link

name = input("Enter file:")
if len(name) < 1:
    name = "mbox-short.txt"
handle = open(name)

hour_list = list() 
hour_dictionary = dict()

for line in handle:
    line_splited = line.split(" ")
    if line_splited[0] == "From":
        hour_list.append(line_splited[6][0:2])

for hour in sorted(hour_list):
    if hour_dictionary.get(hour, False) == False:
        hour_dictionary[hour] = 1
    else:
        hour_dictionary[hour] = hour_dictionary[hour] + 1

for key, value in hour_dictionary.items():
    print(key, value)

@Inspire2023
Copy link

name = input("Enter file:")
if len(name) < 1:
    name = "mbox-short.txt"
handle = open(name,'r')

counts = dict()

for line in handle: 
    if line.startswith("From "):
        pos = line.find(":")
        time = (line[pos-2 : pos]) #finding hour
        counts[time] = counts.get(time,0) + 1 #creating dictionary

order = sorted(counts.items())
for key, value in order : print(key, value)

    

@AmmadJavaid006
Copy link

Hey guys this is my code on the Q it works nicely but if there is anything that I did wrong or I could have initially avoided pls let me know :)

fname = input("Enter File Name: ")
fhandle = open(fname)
lst = list()
count = dict()
finallist = list()
for line in fhandle:
if line.startswith("From") and not line.startswith("From:"):
line = line.split()
lst.append(line[5])

for hour in lst:
hour = hour.split(":")
finallist.append(hour[0])

for ftime in finallist:
count[ftime] = count.get(ftime, 0) + 1

for x, y in sorted(count.items()):
print(x, y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment