Array of Chars to String

Facundo Batista facundobatista at gmail.com
Tue Apr 19 16:54:04 EDT 2005


On 4/19/05, James Stroud <jstroud at mbi.ucla.edu> wrote:

> astr = "Bob Carol Ted Alice"
> letters = "adB"
> 
> import sets
> alist = [lttr for lttr in astr if lttr in Set(letters)]
> newstr = ""
> for lttr in alist:
>   newstr += lttr

>>> astr = "Bob Carol Ted Alice"
>>> letters = "adB"
>>> s1 = set(astr)   # set is a core data type in Py2.4
>>> s2 = set(letters)
>>> ''.join(s1 & s2)
'aBd'

>>> .    Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/



More information about the Python-list mailing list