[Tutor] Counting help
Scott Oertel
me at scottoertel.info
Tue Aug 23 21:38:20 CEST 2005
Byron wrote:
>Luis N wrote:
>
>
>
>>Ideally, you would put your names into a list or dictionary to make
>>working with them easier. If all you're trying to do is count them
>>(and your list of names is long), you might consider a dictionary
>>which you would use like so:
>>
>>#This is just the first thing I considered.
>>
>>l = ['a list of names']
>>
>>d = {}
>>
>>for name in namelist:
>> if d.has_key(name):
>> x = d.get(name)
>> d[name] = x + 1
>> else:
>> d[name] = 1
>>
>>
>>
>
>100% agreed. I have used this approach before and it works great...
>
>Byron
>
>
>
Thanks for the snipplet, it's perfect for what I'm doing, I wasn't aware
of the has_key() or get(), this is very usefull.
-Scott Oertel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050823/d6a6ff20/attachment.htm
More information about the Tutor
mailing list