473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using wild character

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'.
thank you

Sreeraj

Sep 6 '07 #1
9 18017
On 9/6/07, Sreeraj <sr**********@gmail.comwrote:
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.htm...00000000000000

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

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

Cheers,

--
----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in
Sep 6 '07 #2
i hope this may help you.

countries = ["india","africa","atlanta","artica","nigeria"]
filtered = filter(lambda item: item.startswith('a'), l)

Sep 6 '07 #3
i will this may help you.

countries = ["india","africa","atlanta","artica","nigeria"]
filter(lambda country: country.startswith('a'), countries)

Sep 6 '07 #4
On Sep 5, 10:00 pm, Sreeraj <sreeraj22...@gmail.comwrote:
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'.

thank you

Sreeraj
The most thorough answer would no doubt involve regular expressions,
but they can be unpleasant.

To do a "string*" type wildcard filter as in your request:

myList = ["india","africa","atlanta","artica","nigeria"]

newList = [ item for item in myList if item.startswith("a") ]
To do a "*string" wildcard filter use the endswith() function instead
of startswith() and to do a *string* type wildcard filter use
the find() function -1.

Sep 6 '07 #5
Sreeraj schrieb:
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'.
While the startswith-method others pointed out works, I wanted to direct
you attention to the module fnmatch.

http://www.python.org/doc/current/li...e-fnmatch.html

It will work with real wildcards.

Diez
Sep 6 '07 #6
On Wed, 05 Sep 2007 22:54:55 -0700, TheFlyingDutchman wrote:
To do a "*string" wildcard filter use the endswith() function instead
of startswith() and to do a *string* type wildcard filter use
the find() function -1.
Maybe better the ``in`` operator for the '*string*' type. `str.find()`
will go away in the future.

Ciao,
Marc 'BlackJack' Rintsch
Sep 6 '07 #7
On Sep 6, 12:47 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 05 Sep 2007 22:54:55 -0700, TheFlyingDutchman wrote:
To do a "*string" wildcard filter use the endswith() function instead
of startswith() and to do a *string* type wildcard filter use
the find() function -1.

Maybe better the ``in`` operator for the '*string*' type. `str.find()`
will go away in the future.

Ciao,
Marc 'BlackJack' Rintsch
string.find serves a useful purpose in that it returns the starting
location of the string found, or -1 if not found, so if you wanted to
slice "abdecf" on"c", string.find will tell you where that is.

Sep 6 '07 #8
En Thu, 06 Sep 2007 22:19:56 -0300, TheFlyingDutchman <zz******@aol.com>
escribi�:
The Perl community has an expression "There is more than one way to do
it". As in, Perl is good because you have multiple choices (whether
it's a function/module/class/operator) of how to implement a
particular piece of logic. More choices is often good, but this can
lead to a problem in that you might be presented with more things to
learn and or you come across less common ways of doing something that
you are not familiar with in code you are trying to understand.

Does the Python community have a position regarding duplicate ways in
the language to achieve something in your code?
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
pyimport this

--
Gabriel Genellina

Sep 7 '07 #9
On Thu, 06 Sep 2007 16:48:31 -0700, Zentrader wrote:
On Sep 6, 12:47 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Wed, 05 Sep 2007 22:54:55 -0700, TheFlyingDutchman wrote:
To do a "*string" wildcard filter use the endswith() function instead
of startswith() and to do a *string* type wildcard filter use
the find() function -1.

Maybe better the ``in`` operator for the '*string*' type. `str.find()`
will go away in the future.

string.find serves a useful purpose in that it returns the starting
location of the string found, or -1 if not found, so if you wanted to
slice "abdecf" on"c", string.find will tell you where that is.
But that position is not needed here and I think::

result = [name for name in names if 'spam' in name]

reads more natural than::

result = [name for name in names if name.find('spam') -1]

Ciao,
Marc 'BlackJack' Rintsch
Sep 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ and have some base C knowledge. So I know how to solve a given problem, by I sometimes tend to fall back to C. That is what I want to avoid in my current project....
2
by: Pawe³ | last post by:
Hello! I'm looking for efficient code or site where I can find code for finding one string in another string. String which I search should have "wild" characters like '?' for any one char and...
2
by: jackie | last post by:
Here is my prolem: i try to output wstring with unicode value greater than 255(for each wchar_t) to a file such as test.tmp. Here is my code: wofstream outdata("test.tmp"); wstring s; long...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
4
by: SatishPasala | last post by:
hi I am tring to remove all the wild characters from a string. Is there any short method to remove them in a single go. I am right now tring to replace one by one. Ex (999) 999-9999 I...
5
by: Dave | last post by:
I need to filter an Access 2000 result set in ASP 30 using the ADO recordset.filter. I build the filter in pieces. The first clause of the filter is this... WHERE word LIKE 'S%' ... to...
1
by: =?Utf-8?B?UGxheWE=?= | last post by:
I am trying to use a wild character in the LIKE part of my select statement for connecting to an AS400 database table and can't get it to work properly. I can get it to work when I hard code the...
0
by: fullmetal87 | last post by:
Hi guys, I am having this problem I want to do a search with wild card e.g. ABC will retrieve all the group XYZABC123 or SKDABC or ABC234KD do anyone know the wildcard character to search??...
7
by: W. eWatson | last post by:
Is it possible to do a search for a wild card string in another string. For example, I'd like to find "v*.dat" in a string called bingo. v must be matched against only the first character in bingo,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.