[Tutor] 2 problems in a small script

Ian Witham witham.ian at gmail.com
Sat Oct 13 03:48:19 CEST 2007


On 10/12/07, Dick Moores <rdm at rcblue.com> 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!)

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)]

OR...

my_list = filter(well_formed, my_list)

Ian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071013/36fc063b/attachment.htm 


More information about the Tutor mailing list