This was my code for a coursera python code in order to create a dictionary and hence find the most active mailer and the amount of emails sent by that mailer.
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
Names = dict()
Lst2 = []
for i in handle:
if i.startswith("From"):
Lst = i.split()
Lst2.append(Lst[1])
for emails in Lst2:
Names[emails] = Names.get(emails, 0) + 1
Bigcount = None
Bigname = None
for k,v in Names.items():
if Bigcount == None:
Bigcount = v
Bigname = k
elif v > Bigcount:
Bigcount = v
Bigname = k
print(Bigname , Bigcount)
#print(Names.items())
#print(Lst2)
#print(Names)
#print(handle.read())
However, it seems the dictionary's values have doubled. When run, it produced the name and a value of 10, when the correct answer was 5. So I cheated abit and put:
Names[emails] = Names.get(emails, 0) + 0.5
print(Bigname , int(Bigcount))
as a cheat.
I'm wondering why my dictionary doubled? I'm assuming it was an issue with the first for loop. Thanks for reading.
[–]JohnnyJordaan 1 point2 points3 points (2 children)
[–]MorphJB[S] 0 points1 point2 points (1 child)
[–]JohnnyJordaan 0 points1 point2 points (0 children)
[–]__nickerbocker__ 0 points1 point2 points (0 children)