Counting elements in a list wildcard
Iain King
iainking at gmail.com
Tue Apr 25 04:26:50 EDT 2006
hawkesed wrote:
> If I have a list, say of names. And I want to count all the people
> named, say, Susie, but I don't care exactly how they spell it (ie,
> Susy, Susi, Susie all work.) how would I do this? Set up a regular
> expression inside the count? Is there a wildcard variable I can use?
> Here is the code for the non-fuzzy way:
> lstNames.count("Susie")
> Any ideas? Is this something you wouldn't expect count to do?
> Thanks y'all from a newbie.
> Ed
Dare I suggest using REs? This looks like something they'de be good
for:
import re
def countMatches(names, namePattern):
count = 0
for name in names:
if namePattern.match(name):
count += 1
return count
susie = re.compile("Su(s|z)(i|ie|y)")
print countMatches(["John", "Suzy", "Peter", "Steven", "Susie",
"Susi"], susie)
some other patters:
iain = re.compile("(Ia(i)?n|Eoin)")
steven = re.compile("Ste(v|ph|f)(e|a)n")
john = re.compile("Jo(h)?n")
Iain
More information about the Python-list
mailing list