Re: Using wild character

2007-09-07 Thread Marc 'BlackJack' Rintsch
On Thu, 06 Sep 2007 16:48:31 -0700, Zentrader wrote:

 On Sep 6, 12:47 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 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 onc, 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using wild character

2007-09-06 Thread TheFlyingDutchman
On Sep 5, 10:00 pm, Sreeraj [EMAIL PROTECTED] 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'.

 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using wild character

2007-09-06 Thread Diez B. Roggisch
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/lib/module-fnmatch.html

It will work with real wildcards.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using wild character

2007-09-06 Thread Marc 'BlackJack' Rintsch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using wild character

2007-09-06 Thread Zentrader
On Sep 6, 12:47 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 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 onc, string.find will tell you where that is.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using wild character

2007-09-06 Thread Gabriel Genellina
En Thu, 06 Sep 2007 22:19:56 -0300, TheFlyingDutchman [EMAIL PROTECTED]  
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.
py import this

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Using wild character

2007-09-06 Thread TheFlyingDutchman
On Sep 6, 5:53 pm, Gabriel Genellina [EMAIL PROTECTED] wrote:
 En Thu, 06 Sep 2007 20:48:31 -0300, Zentrader [EMAIL PROTECTED]
 escribi?:

  On Sep 6, 12:47 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:

  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 onc, string.find will tell you where that is.

 PEP3100 says it will be removed, but at the same time says [UNLIKELY]...
 http://www.python.org/dev/peps/pep-3100/#id36

 partition serves almost the same purpose and its easier to use.

 --
 Gabriel Genellina

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?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using wild character

2007-09-06 Thread Gabriel Genellina
En Thu, 06 Sep 2007 20:48:31 -0300, Zentrader [EMAIL PROTECTED]  
escribi�:
 On Sep 6, 12:47 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:

 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 onc, string.find will tell you where that is.

PEP3100 says it will be removed, but at the same time says [UNLIKELY]...
http://www.python.org/dev/peps/pep-3100/#id36

partition serves almost the same purpose and its easier to use.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Using wild character

2007-09-05 Thread Amit Khemka
On 9/6/07, Sreeraj [EMAIL PROTECTED] 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#SECTION00714

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using wild character

2007-09-05 Thread [EMAIL PROTECTED]
i hope this may help you.

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using wild character

2007-09-05 Thread [EMAIL PROTECTED]
i will this may help you.

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

-- 
http://mail.python.org/mailman/listinfo/python-list