[Tutor] Counting help

Jacob S. keridee at jayco.net
Thu Sep 1 02:20:00 CEST 2005


I'll do this again, just because I like sending email.

Very similar to Alan G.'s -- but with the do first, ask forgiveness later

a = ["Joe Smith", "Joe Smith", "Jack Smith", "Sam Love", "Joe Smith"]
b = {}
for x in a:
    try: b[x] += 1
    except KeyError: b[x] = 1

Access count like this, of course,

>>> b["Joe Smith"]
3
>>>

Jacob Schmidt

----- Original Message ----- 
From: "Scott Oertel" <me at scottoertel.info>
To: <Tutor at python.org>
Sent: Tuesday, August 23, 2005 1:01 PM
Subject: [Tutor] Counting help


>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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list