Using wild character

Amit Khemka khemkaamit at gmail.com
Thu Sep 6 07:40:57 CEST 2007


On 9/6/07, Sreeraj <sreeraj22283 at gmail.com> wrote:
> hi,
>
>  I am a beginner in Python. I wish to know how can i filter a list of
> strings using wild characters.ie
> Lets say i have list countries =
> ["india","africa","atlanta","artica","nigeria"]. I need only the list
> of string starting with 'a'.

There are a few ways of doing so. For some simple operations there are
functions on the strings, If you want some more complex "filtering"
then have a look at Python's regular expression module "re".

example:
>>> l = ["india","africa","atlanta","artica","nigeria"]
>>> al = [c for c in l if c.startswith('a')]     # this is a list comprehension
>>> al
['africa', 'atlanta', 'artica']

To know more about list comprehensions, have a look at:
http://docs.python.org/tut/node7.html#SECTION007140000000000000000

Methods on strings:
http://docs.python.org/lib/string-methods.html#string-methods

Btw, not all of names in your list are countries !

Cheers,





-- 
----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in



More information about the Python-list mailing list