Created
October 7, 2017 14:43
-
-
Save MichelleDalalJian/0f7d3e76535142aa52377a1d48ff0600 to your computer and use it in GitHub Desktop.
9.4 Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times th…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fname = input("Enter file:") | |
if len(fname) < 1 : name = "mbox-short.txt" | |
hand = open(fname) | |
lst = list() | |
for line in hand: | |
if not line.startswith("From:"): continue | |
line = line.split() | |
lst.append(line[1]) | |
counts = dict() | |
for word in lst: | |
counts[word] = counts.get(word,0) + 1 | |
bigcount = None | |
bigword = None | |
for word,count in counts.items(): | |
if bigcount is None or count > bigcount: | |
bigcount = count | |
bigword = word | |
print (bigword,bigcount) |
name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name, "r")
counts = dict()
big_value = None
big_Key = None
for line in handle:
if line.startswith("From:"):
words = line.split()
word = words[1]
word = word.strip()
counts[word] = counts.get(word, 0) + 1
#print(counts)
for key, value in counts.items():
if big_value is None or big_value < value:
big_key = key
big_value = value
print(big_key,big_value)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fname = input("Enter file:")
if len(fname) < 1 : name = "mbox-short.txt"
hand = open(fname)
lst = list()
for line in hand:
if not line.startswith("From:"): continue
line = line.split()
lst.append(line[1])
counts = dict()
for word in lst:
counts[word] = counts.get(word,0) + 1
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigcount = count
bigword = word
print (bigword,bigcount)