"Andrew Nelsen" <[EMAIL PROTECTED]> wrote

>I was wondering, recently, the most expedient way to take a string 
>with
> [EMAIL PROTECTED]&*] and alpha-numeric characters [ie. "[EMAIL 
> PROTECTED]@*$g@)$&^@&^$F"] 
> and
> place all of the letters in a string or list. I thought there could 
> be
> obvious ways:

The most obvious way is to use the filter() function

newstr = filter(lambda c: c.isalpha(), oldstr)

filter filters outs values from a sequence that match the given 
operation.

or using list comprehensions:

newstr = ''.join([c for c in oldstr if c.isalpha()])


BTW, I know this wasn't real code but...

> import string

you shouldn't use the string module now, string objects make
it nearly redundant

> list = {}

this defines a dictionary not a list.
and the world list is a bui8lt in function, if you use it as a
name you will not be able to convert things to lists!

> string = "@*&^$&[EMAIL PROTECTED](&@$*(&[EMAIL PROTECTED](*&*(&c^&%&^%"

string is the name of the module you imported, if you use
a name thats already in use you will hide that name, in
this case you wouldn't be able to use the string module!

> for x in string:
>    if x <is in string.letters?>
>        list = list + [x]

This would almost work, except the append() method would probably
be better than list addition. In fact its almost exactly what my list
comprehension does above.

> B) Delete all the characters in the string that don't match 
> string.letters:
>
> No idea...strip()?

del() for a list, but you can't modify strings in place so youd
need to convert to a list and then back again. The first method is 
better.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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

Reply via email to