[Tutor] Dictionary Issue

Alan Gauld alan.gauld at btinternet.com
Thu Aug 6 21:24:29 CEST 2015


On 06/08/15 18:17, Ltc Hotspot wrote:

>>> Replace  count[address]= count.get(address,0) +1 with  c =
>>> Counter(['address'])?

You didn't do what Mark suggested.
Did you read the documentation about Counter?

>> NameError
>> Traceback (most recent call last)
>>        2 handle = open (fname, 'r')
>> ----> 3 c = Counter(['address'])
>>
> NameError: name 'Counter' is not defined

So where do you define Counter? Do you import the module?
Do you import Counter from the module?
It's not shown in your code.

> View revised code here:
>
> fname = raw_input("Enter file name: ")
> handle = open (fname, 'r')
> c = Counter(['address'])
>
> count = dict ()
> maxval = None
> maxkee = None
>
> for kee, val in count.items():
>          maxval = val
>          maxkee = kee
>
> for line in handle:
>      if line.startswith("From: "):
>          address = line.split()[1]
>          count[address]= count.get(address,0) +1
> print maxkee and maxval

You have introduced an 'and' here which makes no sense.
It will try to print the logical AND of the two values.
That's not what you want.

Please try to work through this manually and see
how it works (or doesn't). There is no point in folks
making suggestions for improvements until you
understand how it should work yourself.b You have
all the components needed to build the solution,
now its up to you to fit them together such that
they work. We can make suggestions but you need
to solve the problem, we can't, and won't do it
for you.

-- 
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




More information about the Tutor mailing list