At 06:48 PM 10/12/2007, Ian Witham wrote:


On 10/12/07, Dick Moores <[EMAIL PROTECTED]> wrote:
Please see the code and it's output here:
< http://www.rcblue.com/Python/buggy_For_Web.py>


I'm attempting to eliminate the elements (strings) of lstA that are
not well-formed in that they contain at least one character that is
not in the string astr.


 
Hi Dick,

If your interested, this could be done succinctly with set types. (no iterating required!)

Hey, I'm VERY interested.

def well_formed(word):
    return not set('#.<@') & set(word)

>>> well_formed('porkpie')
True
>>> well_formed('p#rkpie')
False

And then it seems you are coming to terms with list comprehensions...

my_list = [word for word in my_list if well_formed(my_word)]

Done. I had no idea set could be used that way! With a couple of modifications, I used your ideas in both <buggy_For_Web5b.py> and <getCrew_ForWeb2.py> (see the thread I started, < http://www.nabble.com/An-idea-for-a-script-tf4603558.html#a13149808 >).

OR...

my_list = filter(well_formed, my_list)

I'll look into "filter".

A question:

Here's my(yours) well_formed():

def well_formed(word, astr):
    """
    Returns True if set(<a string>) - set(<a string>) is empty; False if not.
    """
    return not set(word) - set(astr)

Is it OK to use <a string> like that in a docstring?

Thanks, Ian.

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

Reply via email to