Use of Dictonary in python -


i doing coursera python exercise , having trouble writing code.

the question following:

write program read through mbox-short.txt , figure out has sent greatest number of mail messages. program looks 'from ' lines , takes second word of lines person sent mail.

the program creates python dictionary maps sender's mail address count of number of times appear in file. after dictionary produced, program reads through dictionary using maximum loop find prolific committer. sample text file in line: http://www.pythonlearn.com/code/mbox-short.txt

and expected output should be:

cwen@iupui.edu 5

this code:

 name = raw_input("enter file:") if len(name) < 1 : name = "mbox-short.txt" name="mbox-short.txt" handle=open(name) text=handle.read() line in handle:     line=line.rstrip()     words=line.split()     if words==[]: continue     if words[0]!='from':continue     words2=words[1] words3=words2.split() counts=dict() word in words3:      counts[word]=counts.get(word,0)+1    bigcount=none bigword=none key,val in counts.items():  if val>bigcount:     bigword=key     bigcount=val print bigword,bigcount 

my output is: cwen@iupui.edu 1

please suggest error in code.

here code need, you're not storing words2 output in list, , mentioned in comments you're recursing file in wrong manner too.

hope you.

name = raw_input("enter file:") if len(name) < 1 : name = "mbox-short.txt" name="mbox-short.txt" handle=open(name) words3 = [] line in handle:     line=line.rstrip()     words=line.split()     if words==[]: continue     if words[0]!='from':continue     words2=words[1]     words3.append(words2.split()[0])     # print words counts=dict() word in words3:      counts[word]=counts.get(word,0)+1   bigcount=none bigword=none key,val in counts.items():  if val>bigcount:     bigword=key     bigcount=val print bigword,bigcount 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -