[Tutor] Dictionary Issue
Ltc Hotspot
ltc.hotspot at gmail.com
Thu Aug 6 04:27:42 CEST 2015
Hi Alan
The output as reported by the latest code revision: cwen at iupui.edu 1 ←
Mismatch
Looks like python continues to print the wrong data set:
In [11]: print val
1 In [12]: print kee ray at media.berkeley.edu In [13]: print address
cwen at iupui.edu
In order to complete the assignment, using data from the source file,
python must print the email address of the maximum sender and the number
of sends, i.e., cwen at iupui.edu 5
I think the problem is in the placement of the counter?
Question: What is the source of the dictionary keys and values:
maxval = None
maxkee = None
Here is the latest revised code as follows:
fname = raw_input("Enter file name: ")
handle = open (fname, 'r')
count = dict ()
for line in handle:
if line.startswith("From: "):
address = line.split()[1]
count[address]= count.get(address,0) +1
maxval = None
maxkee = None
for kee, val in count.items():
maxval = val
maxkee = kee
print address, val
Hal
On Wed, Aug 5, 2015 at 6:21 PM, Alan Gauld <alan.gauld at btinternet.com>
wrote:
> On 05/08/15 23:58, Ltc Hotspot wrote:
>
> fname = raw_input("Enter file name: ")
>> handle = open (fname, 'r')
>> for line in handle:
>> if line.startswith("From: "):
>> address = line.split()[1]
>>
>>
> So far so good.
>
>
>> ## 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.
>>
>> count = dict()
>>
>
> But here you create a brand new dictionary.
> Every time you go round the loop.
> And it wipes out the old one.
> You need to move that out of the loop.
>
> for wrd in address:
>>
>
> address is a string. So wrd will be set to every
> character in the string. I don;t think that's what
> you want?
>
> count[wrd]= count.get(wrd,0) +1
>>
>> ## After the dictionary is produced, the program reads
>> ## through the dictionary using a maximum loop to
>> ## find the greatest number of mail messages.
>>
>> maxval = none
>> maxkee = none
>>
>
> See my previous email. none should be None.
> Case matters in Python.
>
> for kee, val in count.items():
>> if maxval == none or maxval <val:
>> maxval = val
>> maxkee = kee
>>
>>
>> #items are printed
>>
>> print address
>>
>
> Notice that address gets reset every time the loop reads
> a new line so this will only print the last address.
> But maybe that's what you wanted?
>
> --> Did I resolve the reset in the revised code?
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list