Skip to content

Instantly share code, notes, and snippets.

@MichelleDalalJian
Created October 7, 2017 14:43
Show Gist options
  • Save MichelleDalalJian/0f7d3e76535142aa52377a1d48ff0600 to your computer and use it in GitHub Desktop.
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…
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)
@Boy-Davis
Copy link

Boy-Davis commented Jul 6, 2022

.

this indicates the highest occurrence of that particular sender's email address

@damonshijie
Copy link

Is it because the list is automatically sorted? and which line indicates that?

@Dennis-hub-star
Copy link

Hi everyone. I do not know what's wrong with my code. Kindly help me
Assignment 9 4

@damonshijie
Copy link

u must separate the From: and From

@MaryamFarshbafi
Copy link

name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
dic=dict()
for line in handle:
if not line.startswith("From:"): continue
line = line.split()
if line[1] in dic.keys():
dic[line[1]]+=1
else:
dic[line[1]]=1
line=[]

largest=-1

for key,value in dic.items():
if largest< value:
largest=value
key_max=key
print(key_max,largest)

@faranakR
Copy link

name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
dic = {}
for line in handle:
line = line.rstrip().split()
if "From" in line:
key = line[1]
if key not in dic:
dic[key] = 1
else:
dic[key] += 1
value_list = []
for key, value in dic.items():
if value == max(dic.values()):
ans1 = key
ans2 = value
print(ans1, ans2)

@desquivelre
Copy link

fname = open("mbox-short.txt")

dtc = dict()
update_dtc = dict()

for line in fname:
if line.startswith("From "):
linea = line.strip().split()
if linea[1] in dtc:
update_dtc[linea[1]] = int(dtc.get(linea[1])) + 1
dtc.update(update_dtc)
else:
update_dtc[linea[1]] = 1
dtc.update(update_dtc)

max_value = max(dtc.values())

for element in dtc:
if dtc[element] == max_value:
correo = element
result = dtc.get(element)

print(str(correo) + " " + str(result))

@rishabhrathore055
Copy link

name = input("Enter file:")
counts = dict()
if len(name) < 1:
    name = "mbox-short.txt"
handle = open(name)
for line in handle:
    if line.startswith('From '):
        words = line.split()[1]
        counts[words] = counts.get(words,0) + 1
maxcount = None
maxword = None
for words,count in counts.items(): 
    if maxcount is None or count > maxcount:
        maxcount = count
        maxword = words

print (maxword,maxcount)

 
    
        
                    

@toludoyin
Copy link

toludoyin commented Oct 21, 2022

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

db = list()
for word in handle:
    word= word.strip()
    if not word.startswith('From:'):
        continue
    word = word.split()
    word = word[1]
    db.append(word)

    counts = dict()
    for rows in db:
        counts[rows] = counts.get(rows,0) + 1

max_count = None
max_word = None
for word, count in counts.items():
    if max_count is None or count > max_count:
        max_count = count
        max_word = word
print(max_word,max_count)

@PixelPusher42
Copy link

Anyone knows why my code is not working? output is "[email protected] 10" instead of
"[email protected] 5". I think it is being double counted.

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

lst = list()
for line in handle:
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

bigword = None
bigcount = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count

print(bigword,bigcount)

@amankrs21
Copy link

Mine 💯
image

@KarenTheEarth
Copy link

Screenshot 2023-04-07 at 9 22 23 am

@ShuckZ77
Copy link

name = input("Enter file:")

handle = open(name)

leest = list()

for line in handle:
if line.startswith('From '):

    line2=line.strip()
    
    line3=line2.split()
    
    line4=line3[1]
    
    leest.append(line4)
    
    #print(line4)

#print(leest)

dct = dict()

for email in leest:

dct[email] = dct.get(email,0) + 1

#print(dct)

dct_items = dct.items()

#print(dct_items)

coutmail = None
countcount = None

for K,V in dct_items:
if countcount is None or V>countcount:

    countmail=K
    countcount=V

print(countmail,countcount)

@ssaass090904
Copy link

ssaass090904 commented Apr 23, 2023

This is mine:

name = input("Enter file:")
handle = open(name)
counts = dict()

for line in handle:
if line.startswith('From:'):
words = line.split()
word = words[1]
else: continue
counts[word] = counts.get(word, 0) + 1

bigcount = None
bigword = None
for aaa, bbb in counts.items():
if bigcount is None or bbb > bigcount:
bigcount = bbb
bigword = aaa
print('The most sender is' , bigword,'with this number --->' , bigcount)

@KamdiaSJ
Copy link

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)

@Inspire2023
Copy link

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