On Sat, 3 Dec 2005, Srinivas Iyyer wrote:
> >>> a
> ['apple', 'boy', 'boy', 'apple']
>
> >>> b
> ['Apple', 'BOY', 'APPLE-231']
>
> >>> for i in a:
>       pat = re.compile(i,re.IGNORECASE)
>       for m in b:
>               if pat.match(m):
>                       print m


Hi Srinivas,

We may want to change the problem so that it's less focused on "print"ing
results directly.  We can rephrase the question as a list "filtering"
operation: we want to keep the elements of b that satisfy a certain
criteron.


Let's give a name to that criterion now:

######
def doesNameMatchSomePrefix(word, prefixes):
    """Returns True if the input word is matched by some prefix in
    the input list of prefixes.  Otherwise, returns False."""
    # ... fill me in

######


Can you write doesNameMatchSomePrefix()?  In fact, you might not even need
regexes to write an initial version of it.



If you can write that function, then what you're asking:

> I do not want python to print both elenents from lists a and b.  I just
> want only the elements in the list B.

should not be so difficult: it'll be a straightforward loop across b,
using that helper function.



(Optimization can be done to make doesNameMatchSomePrefix() fast, but you
probably should concentrate on correctness first.  If you're interested in
doing something like this for a large number of prefixes, you might be
interested in:

    http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

which has more details and references to specialized modules that attack
the problem you've shown us so far.)


Good luck!

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to