Re: partial / wildcard string match in 'in' and 'list.index()'

Discussion in 'Python' started by Patrick Hall, May 28, 2004.

  1. Patrick  Hall

    Patrick Hall Guest

    Hi,

    > For a given list:
    > fruits=["apples","oranges","mangoes","bananas"]
    >
    > Is it possible to do wildcard matches like shown below?
    > 1. "man*" in fruits
    > 2. fruits.index("man*")
    > 3. "*nanas*" in fruits
    > 4. fruits.index("*nanas")


    I'm not sure if this is what you had in mind, but you can use list
    comprehensions:

    fruits=["apples","oranges","mangoes","bananas"]
    import re
    1. [fruit for fruit in fruits if re.match("man.*",fruit)]
    2. [fruits.index(fruit) for fruit in fruits if re.match("man.*",fruit)]
    3. [fruit for fruit in fruits if re.match(r".*an.*",fruit)]
    4. [fruits.index(fruit) for fruit in fruits if re.match("man*",fruit)]

    Note also that I think you're looking for
    ..*
    instead of just
    *
    in your regular expressions.

    > or is there any way to achieve an equivalent effect
    > short of doing a while loop?


    I personally am not so sure a while loop wouldn't be clearer in this
    case -- things like "fruit for fruit in fruits" are pretty unreadable
    to me.

    Cheers,
    Pat
    Patrick Hall, May 28, 2004
    #1
    1. Advertising

  2. Patrick  Hall

    Jon Perez Guest

    Patrick Hall wrote:

    > Hi,
    >
    >
    >>For a given list:
    >>fruits=["apples","oranges","mangoes","bananas"]
    >>
    >>Is it possible to do wildcard matches like shown below?
    >>1. "man*" in fruits
    >>2. fruits.index("man*")
    >>3. "*nanas*" in fruits
    >>4. fruits.index("*nanas")

    >
    > I'm not sure if this is what you had in mind, but you can use list
    > comprehensions:
    >
    > fruits=["apples","oranges","mangoes","bananas"]
    > import re
    > 1. [fruit for fruit in fruits if re.match("man.*",fruit)]
    > 2. [fruits.index(fruit) for fruit in fruits if re.match("man.*",fruit)]
    > 3. [fruit for fruit in fruits if re.match(r".*an.*",fruit)]
    > 4. [fruits.index(fruit) for fruit in fruits if re.match("man*",fruit)]


    These would work, but I was wondering if there was some compact
    way to get 'in' and lists.index() to do wildcard matching as opposed
    to exact matching.
    Jon Perez, May 28, 2004
    #2
    1. Advertising


  3. > These would work, but I was wondering if there was some compact
    > way to get 'in' and lists.index() to do wildcard matching as opposed
    > to exact matching.


    Short answer: no.

    Long answer:
    There is a regular expression library, aptly called 're', for non-exact
    string searching.

    The standard string methods only do exact searching likely because there
    are algorithms that make it fast (linear in the length of the searched
    string), that are also quite small.

    There exist very simple looking regular expressions that force searches
    to take exponential (in the length of the pattern) time to search, and
    are generally fairly large.

    An example of an exponentially slow regular expression is contained in
    the following example...
    >>> import re
    >>> import time
    >>> def test(n):

    .... a = 100*'0'
    .... t = time.time()
    .... re.search('(0+)'*n, a)
    .... return time.time()-t
    ....
    >>> for i in xrange(18, 23):

    .... print i, test(i)
    ....
    18 0.141000032425
    19 0.266000032425
    20 0.530999898911
    21 1.07899999619
    22 2.1400001049
    >>>


    Trust me, you don't want that kind of slow down when you are searching
    in strings with "if i in 'somestring':".


    - Josiah
    Josiah Carlson, Jun 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruce Lawson

    [ ] (Wildcard - Character(s) to Match) ?

    Bruce Lawson, Apr 19, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    434
    Karl Seguin [MVP]
    Apr 19, 2006
  2. Jon Perez
    Replies:
    1
    Views:
    1,056
    Peter Otten
    May 28, 2004
  3. Mr.SpOOn

    wildcard match with list.index()

    Mr.SpOOn, Nov 10, 2008, in forum: Python
    Replies:
    4
    Views:
    285
    Sion Arrowsmith
    Nov 19, 2008
  4. Replies:
    7
    Views:
    820
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    270
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page