Extract all words that begin with x

superpollo utente at esempio.net
Mon May 10 05:41:08 EDT 2010


superpollo ha scritto:
> Jimbo ha scritto:
>> Hello
>>
>> I am trying to find if there is a string OR list function that will
>> search a list of strings for all the strings that start with 'a' &
>> return a new list containing all the strings that started with 'a'.
>>
>> I have had a search of Python site & I could not find what I am
>> looking for, does a function like this exist?
> 
>  >>> sw = lambda s: lambda t: t.startswith(s)
>  >>> list = ["a string","another one","this is a string","and so is this 
> one"]
>  >>> filter(sw("a"),list)
> ['a string', 'another one', 'and so is this one']
>  >>>
> 
> bye

of course there is a simpler way:

 >>> [string for string in list if string.startswith("a")]
['a string', 'another one', 'and so is this one']
 >>>

bye



More information about the Python-list mailing list