comparing all values of a list to regex
Delaney, Timothy
tdelaney at avaya.com
Wed Sep 25 19:36:18 EDT 2002
> From: Alex Martelli [mailto:aleax at aleax.it]
>
> num_matches = [ there.match(item) is not None for item in
> alist ].count(1)
>
> if num_matches == 0:
> againanothernewlist.append(alist)
> elif num_matches == 1:
> anothetnewlist.append(alist)
> elif num_matches == len(alist):
> anewlist.append(alist)
I would actually prefer to do this another way.
match_all = []
match_one = []
match_some = []
match_none = []
# Expanded out for clarity - could be one line
matches = map(regexp.match, alist)
matches = filter(None, matches)
matches = len(matches)
# Find out which list to append to
match_list = match_all
if not matches:
match_list = match_none
elif matches == 1:
match_list = match_one
elif matches != len(alist):
match_list = match_some
match_list.append(alist)
Note that I added an additional case - where more than one matched, but not
all.
Tim Delaney
More information about the Python-list
mailing list