[Tutor] Counting help

Luis N tegmine at gmail.com
Tue Aug 23 21:10:25 CEST 2005


On 8/23/05, Scott Oertel <me at scottoertel.info> wrote:
> I have extracted a list of names, i.e.
> 
> "Joe Smith"
> "Joe Smith"
> "Jack Smith"
> "Sam Love"
> "Joe Smith"
> 
> I need to be able to count the occurances of these names and I really
> don't have any idea where to begin.
> 
> Any ideas?  excuse me this is my first post to this list, I hope I
> included enough information.
> 
> 
> -Scott Oertel

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      

Luis


More information about the Tutor mailing list