[Tutor] Dictionary Issue
Ltc Hotspot
ltc.hotspot at gmail.com
Thu Aug 6 06:22:13 CEST 2015
Mark,
Replace count[address]= count.get(address,0) +1 with c =
Counter(['address'])?
Regards,
Hal
On Wed, Aug 5, 2015 at 9:03 PM, Mark Lawrence <breamoreboy at yahoo.co.uk>
wrote:
> On 05/08/2015 23:58, Ltc Hotspot wrote:
>
>> Hi Mark,
>>
>> Address identifies the email address with the maximum number of sends:
>> cwen at iupui.edu.
>>
>> Secondly, we are missing a count on the number of messages sent by
>> cwen at iupui.edu, i.e., 5.
>>
>> Thirdly, maxval 'none' is not defined on line # 24
>>
>> Questions: How do we define the value of none for the key maxval and
>> retrieve a number count on the number of messages sent by cwen at iupui.edu.
>>
>>
>> NameError:
>>
>> Traceback (most recent call last)
>> C:\Users\vm\Desktop\apps\docs\Python\assignment_9_4_5.py in <module>()
>> 22 ## find the greatest number of mail messages.
>> 23
>> ---> 24 maxval = none
>> 25 maxkee = none
>> 26 for kee, val in count.items():
>>
>> NameError: name 'none' is not defined
>>
>> In [52]: print address
>> cwen at iupui.edu
>>
>> Revised data:
>>
>>
>> ## The program looks for 'From ' lines and takes the second
>> ## word of those lines as the person who sent the mail.
>>
>> fname = raw_input("Enter file name: ")
>> handle = open (fname, 'r')
>> for line in handle:
>> if line.startswith("From: "):
>> address = line.split()[1]
>>
>>
>> ## 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()
>> for wrd in address:
>> 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
>> for kee, val in count.items():
>> if maxval == none or maxval <val:
>> maxval = val
>> maxkee = kee
>>
>>
> You can greatly simplify all of the above code if you use a Counter from
> the collections module
> https://docs.python.org/3/library/collections.html#collections.Counter
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> _______________________________________________
> 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