Python: Predicates, filter() and List Comprehension
Lately, I've been playing with some of the functional elements of Python. Here's a quick example of predicates, the filter(function, sequence) function and list comprehension.
I have a list of emails and I want to return only those from a specific domain. To do that, I wrote a CheckDomain(string) function that's only job is to tell me if the email I pass to it is or is not from the domain I'm looking for. Because our CheckDomain(string) function returns a boolean, it's referred to as a predicate.
## Function to check the domain of an email
def CheckDomain(email):
if email.find(domainName) > -1:
return True
else:
return False
Of course, we'll need a list of emails and a domain name to search for
## Our initial list of emails
listOfEmails = ['mwaddams@innotech.com',
'janet@interplanet.com', 'tsmykowski@innotech.com',
'pgibbons@innotech.com', 'sven@svenson.com',
'bob@roberts.com', 'spam@spamola.com']
## domain name we're interested in filtering on
domainName = '@innotech.com'
Now let's try our first search...
## our filtered list of emails will go here
filteredEmails = []
## loop through our emails and check the domain
for email in listOfEmails:
if (CheckDomain(email)):
filteredEmails.append(email)
print filteredEmails
>>> ['mwaddams@innotech.com',
'tsmykowski@innotech.com',
'pgibbons@innotech.com']
The code is straight forward: declare a variable, loop through the emails, check each email for the presence of a particular string and then add it to the list if it does. Fortunately, the filter(function, sequence) can help solve this problem
## our filtered list of emails will go here
filteredEmails = filter(CheckDomain, listOfEmails)
print filteredEmails
>>> ['mwaddams@innotech.com',
'tsmykowski@innotech.com',
'pgibbons@innotech.com']
With the filter(function, sequence) function, we didn't have to initialize the filteredEmails sequence nor did we have to manually loop through the listOfEmails sequence. It was just point and shoot. But let's say we want to do more than just filter. Maybe we want to apply some operation to each item in the sequence. List comprehension is just the thing we need.
## get all usernames
usernames= [email[0:email.index("@")]
for email in listOfEmails]
print usernames
>>> ['mwaddams', 'janet', 'tsmykowski',
'pgibbons', 'sven', 'bob', 'spam']
Still need to filter? Like the filter function, list comprehension makes use of predicates:
## get innotech usernames only
filteredUsernames = [email[0:email.index("@")]
for email in listOfEmails if CheckDomain(email)]
## EDITED ON 2/27/2008 TO CORRECT TYPO
print filteredEmails
print filteredUsernames
>>> ['mwaddams', 'tsmykowsk', 'pgibbons']






0 Comments:
Post a Comment
<< Home