ex 9.4 python coursera

efish picture efish · Jun 3, 2018 · Viewed 7.6k times · Source

Currently working on this for Coursera's Python For everybody class. This is ex 9.4. For some reason the emcount is double what it is supposed to be when ran. The problem seems to start early on since line has double the amount it should. Does anyone have an idea of where I went wrong? Thanks!

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

handle = open(name)

lst = list()
#emcount = dict()
for line in handle:
    if not line.startswith("From"): continue
    line=line.split()
    print(line)
    lst.append(line[1])#adding each email occurrence to lst
#    print(lst)
emcount = dict()
for word in lst:
    emcount[word] = emcount.get(word,0)+1
#    print(emcount)

bigcount = 0#empty at beginning
bigword = None
for word,count in emcount.items():
    #items give you acopy of each key value pair, word is the key
    if bigcount> count:
        bigcount = bigcount
        bigword = bigword
        print(bigword, bigcount)
    else:
        bigcount = count
        bigword = word


`

Answer

Paul Zaino picture Paul Zaino · Jun 14, 2018

I worked on your code, and made modifications to make it work.

Making a list was not needed. Just put data straight into a dict.

The else statements were not needed. You want to keep checking if the new value is larger then the old value, and if so, then make it the new value.

Bigcount and bigword both needed to be set to None before the if statement

Also, you need a second condition in the if statement if the value is None for the first pass.

And moved the print of bigword and bigcount outside the for loop to only print the result one time.

One more thing, the assignment says to find all lines with "From ", not lines with "From". See the difference?

I hope this helps.

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
emcount = dict()
for line in handle:
    if not line.startswith("From "): continue
    line = line.split()
    line = line[1]
    emcount[line] = emcount.get(line, 0) +1 
bigcount = None
bigword = None
for word,count in emcount.items():
if bigcount == None or count > bigcount:
    bigcount = count
    bigword = word
print(bigword, bigcount)