TransWikia.com

Nested for loop using dict

Stack Overflow Asked by PURWU on December 5, 2021

I am doing a Coursera python exercise and having trouble writing my code.

The question is as following:

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 they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

The sample text file is in this line:
http://www.pythonlearn.com/code/mbox-short.txt

And the expected output should be:

[email protected] 5

This is my code:

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

for line in handle:
    word = line.split()
    if line.startswith('From '):
        email = word[1]
        for sender in email:
            if sender not in count:
                count[sender] = count.get(sender, 0) + 1

bigcount = None
bigname = None
for name,count in count.items():
    if bigname is None or count > bigcount:
        bigname = name
        bigcount = count
print bigname, bigcount

The output I have is:

. 1

I think there is something wrong in "for sender in email" part, but couldn’t figure out how it results in the undesired output.

9 Answers

counts = dict()
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
fhand = open(name)
for line in fhand:
       line = line.rstrip()
       if not line.startswith('From ') : continue
       words = line.split()            
       counts[words[1]]=counts.get(words[1],0)+1 
st = 0 
for k in counts:
       if counts[k] > st :
       st = counts[k]
       addy = k
print (addy, st)

Answered by Steve Kinzey on December 5, 2021

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

words = list()

for line in handle:
    if not line.startswith("From:") : continue
    line = line.split()
    words.append(line[1])


counts = dict()

for word in words:
           counts[word] = counts.get(word, 0) + 1 


maxval = None
maxkey = None
for key,val in counts.items() :
#   if maxval == None : maxval = val
  if val > maxval:
      maxval = val
      maxkey = key   

print (maxkey, maxval)

Answered by Mohamed Atef on December 5, 2021

I am taking the same Coursera Python course. Since I am new at it, I am sharing my code for the Assignment. To me the key part was first to use if not line, then split it.

counts=dict()
fname=input('Enter file: ')
if len(fname)<1:
    fname='mbox-short.txt'
else:
    print('Error')
    quit()
fhand=open(fname)

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

key=None
num=0
for k,v in counts.items(): 
    if key is None or v > num:
        num=v
        key=k
print (num, key)

Answered by ghv8 on December 5, 2021

fname=input('enter the file name: ')
d=dict()
try:
    fhand=open(fname,'r')
except:
    print('file not found')
    exit()

for line in fhand: 

    if line.startswith("From:"):
        srt=line.find(' ')
        sl=line[srt:-1]

        if sl not in d:
            d[sl]=1
        else:
            d[sl]+=1

print(d)
largest= 0
email=''

for key in d:
    if d[key] > largest:
        largest=d[key]
        email=key
print(email,': ',largest)

Answered by Mahesvar Tr on December 5, 2021

fname = input("Enter The File Name")
fhandle = open(fname,'r')

sender = dict()
for line in fhandle:
    if line.startswith("From "):
        sender[line.split()[1]] = sender.get(line.split()[1],0) + 1

max_key = None
max_val = None

for key,value in sender.items():
    if max_val is None or max_val < value :
        max_val = value
        max_key = key

print(max_key,max_val)

Answered by Abhishek Raha on December 5, 2021

name = input("Enter the file name:")
handle = open(name)
new = dict()
#count = 0
for line in handle:
 word = line.split()
 if line.startswith("From "):
    new[word[1]] = new.get(word[1],0) + 1
largest = 0
email = None
for k,v in new.items():
 if email is None or v > largest:
    largest = v
    email = k
print (email,largest)

Answered by Anupriya Krishnamoorthy on December 5, 2021

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

#fl=open('C:UsersAlgoritmDocumentsPython Courserambox-short.txt')
lst=list()
count=dict()

#scan the file and create a list
for lines_in_the_file in fl:
xx=lines_in_the_file.rstrip().split()
if not lines_in_the_file.startswith('From '): continue #if in the line keep it
word=lines_in_the_file.split()
#print word[1]
xx=word[1]
#for index in xx: #find repeted words in the list Word
lst.append(xx)

#print lst
lis=lst
for x in lis:
count[x]=count.get(x,0)+1

#print  count
bigcount=None
bigwords=None



for x, y in count.items():
if bigcount is None or y>bigcount:
   bigwords=x
   bigcount=y
print bigwords, bigcount

Answered by Machine Learning XL on December 5, 2021

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
words = list()
counts = dict()

for line in handle:
    words = line.split()
    if words == []: continue
    if words[0] != 'From': continue
    counts[words[1]] = counts.get(words[1],0) + 1
    #print counts


maxval = None
maxkey = None

for kee, val in counts.items():
    if maxval == None: maxval = val
    if maxval < val: 
        maxval = val
        maxkey = kee
print maxkey, maxval

Answered by ASi on December 5, 2021

The following loop is not appropriate in this situation because you are basically iterating over all the characters of the email address.

for sender in email:
   ...

That is why you are getting a character . when you print the email address with the largest count. You can easily see the effects once you print the count at the end of the loop.

Following checking is also redundant as you are implicitly checking it when you are getting the dictionary value with get method.

if sender not in count:
   ...

So, the final corrected code should be something like this.

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

for line in handle:
    word = line.split()
    if line.startswith('From '):
        count[word[1]] = count.get(word[1], 0) + 1
largest = 0
email = ''
for k in count:
    if count[k] > largest:
        largest = count[k]
        email = k
print largest, email

Answered by Hossain Muctadir on December 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP