So the assignment wants me to read a text file, parse out lines that starts with a certain word, throw those words in a list, create a dictionary that puts the words into the dictionary (key) and the amount of times they appear(value). Easy enough. I've got this down.
It then wants you to print out the the key and value of the largest value. This I can't figure out. I've got a loop that prints out the largest value, but I don't also know how to print out the key that goes along with it? Any help?
name = "mbox-short.txt"
handle = open(name)
newdict = dict()
for x in handle:
x = x.rstrip()
x = x.split()
for x in newlist:
newdict[x] = newdict.get(x,0) +1
largest = None
for x in newdict.values():
if largest is None or x > largest:
largest = x
print 'largest value:', x,
print 'The Largest Value is:', largest
The part from the text book that I'm having problems with:
After all the data has been read the program looks through the dictionary using
a maximum loop (see Section 5.7.2) to find who has the most messages and how
many messages the person has.
Figured it out. I wasn't aware that you could run a for loop on two variables(?) eg, for x,y in dictionary.items():