[Tutor] Dictionary Issue

Ltc Hotspot ltc.hotspot at gmail.com
Thu Aug 6 21:05:45 CEST 2015


On my breath and soul, I did:

Counter objects have a dictionary interface except that they return a zero
count for missing items instead of raising a KeyError
<https://docs.python.org/3/library/exceptions.html#KeyError>:
>>>

>>> c = Counter(['eggs', 'ham'])


On Thu, Aug 6, 2015 at 11:59 AM, Mark Lawrence <breamoreboy at yahoo.co.uk>
wrote:

> On 06/08/2015 18:17, Ltc Hotspot wrote:
>
>> On Thu, Aug 6, 2015 at 8:28 AM, Mark Lawrence <breamoreboy at yahoo.co.uk>
>> wrote:
>>
>> On 06/08/2015 05:22, Ltc Hotspot wrote:
>>>
>>> Please don't top post here, it makes following long threads difficult.
>>>
>>> Mark,
>>>
>>>>
>>>> Replace  count[address]= count.get(address,0) +1 with  c =
>>>> Counter(['address'])?
>>>>
>>>>
>>> Try it at the interactive prompt and see what happens.
>>>
>>> How do I define counter,view trace back:
>>>
>>> NameError
>>> Traceback (most recent call last)
>>> C:\Users\vm\Desktop\apps\docs\Python\new.txt in <module>()
>>>        1 fname = raw_input("Enter file name: ")
>>>        2 handle = open (fname, 'r')
>>> ----> 3 c = Counter(['address'])
>>>        4
>>>        5
>>>
>>> NameError: name 'Counter' is not defined
>>
>>
>> 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 obviously haven't bothered to read the link I gave you about the
> Counter class so I give up.
>
> --
> 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