wildcard match with list.index()

Discussion in 'Python' started by Mr.SpOOn, Nov 10, 2008.

  1. Mr.SpOOn

    Mr.SpOOn Guest

    Hi,
    is there any way to search elements in a list using wildcards?

    I have a list of various elements and I need to search for elements
    starting with 'no', extract them and put in a new list.
    I was thinking about something like:

    mylist.index('no*')

    Of course this doesn't work.
    Mr.SpOOn, Nov 10, 2008
    #1
    1. Advertising

  2. Mr.SpOOn <> writes:

    > Hi,
    > is there any way to search elements in a list using wildcards?
    >
    > I have a list of various elements and I need to search for elements
    > starting with 'no', extract them and put in a new list.
    > I was thinking about something like:
    >
    > mylist.index('no*')
    >
    > Of course this doesn't work.


    I have exactly what you need :)

    >>> import fnmatch
    >>> fnmatch.filter(['baba', 'nono', 'papa', 'mama', 'nostradamus'], 'no*')

    ['nono', 'nostradamus']
    >>>


    HTH

    --
    Arnaud
    Arnaud Delobelle, Nov 10, 2008
    #2
    1. Advertising

  3. Mr.SpOOn

    Mr.SpOOn Guest

    Thanks, I just have to choose which one to use :)
    Mr.SpOOn, Nov 11, 2008
    #3
  4. Mr.SpOOn

    jeff Guest

    On Nov 10, 1:59 pm, Arnaud Delobelle <> wrote:
    > Mr.SpOOn <> writes:
    > > Hi,
    > > is there any way to search elements in a list using wildcards?

    >
    > > I have a list of various elements and I need to search for elements
    > > starting with 'no', extract them and put in a new list.
    > > I was thinking about something like:

    >
    > > mylist.index('no*')

    >
    > > Of course this doesn't work.

    >
    > I have exactly what you need :)
    >
    > >>> import fnmatch
    > >>> fnmatch.filter(['baba', 'nono', 'papa', 'mama', 'nostradamus'], 'no*')

    >
    > ['nono', 'nostradamus']
    >
    >
    >
    > HTH
    >
    > --
    > Arnaud


    related to the attached, what if i want to match the entry 'b' as the
    first element as the first item in a list of 0 or more additional
    lists. example is here - i would like to match any item in the outer
    list that has 'b' as its first element, not caring what the additional
    elements contain (but knowing those additional elements will be one or
    more lists):

    >>> list

    [['a', [], []], ['b', [1, 2], []], ['c', [3, 4], [5, 6]]]
    >>> list.index(['b',[],[]])


    ie, would like to match the second element in the list with something
    where i just know 'b' is the first element, but have no idea what the
    other elements will be:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: list.index(x): x not in list
    >>> list.index(['b',[1,2],[]])

    1
    jeff, Nov 18, 2008
    #4
  5. jeff <> wrote:
    >>>> list

    >[['a', [], []], ['b', [1, 2], []], ['c', [3, 4], [5, 6]]]
    >>>> list.index(['b',[],[]])

    >
    >ie, would like to match the second element in the list with something
    >where i just know 'b' is the first element, but have no idea what the
    >other elements will be:
    >
    >Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    >ValueError: list.index(x): x not in list
    >>>> list.index(['b',[1,2],[]])

    >1


    If you really want to do that:

    py> lst.index([x for x in lst if x[0] == 'b'][0])

    (Oh, yeah, don't shadow the builtin "list".)

    What I suspect would be far more useful is a better data structure:

    py> dct = dict((x[0], x[1:]) for x in lst)
    py> dct['b']>>> dct['b']
    [[1, 2], []]

    Dealing with the case of more than one entry identified by 'b' is
    left as a problem to someone who knows what the data actually is.

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    "Frankly I have no feelings towards penguins one way or the other"
    -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
    Sion Arrowsmith, Nov 19, 2008
    #5
    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:
    440
    Karl Seguin [MVP]
    Apr 19, 2006
  2. Jon Perez
    Replies:
    1
    Views:
    1,075
    Peter Otten
    May 28, 2004
  3. Patrick  Hall
    Replies:
    2
    Views:
    1,989
    Josiah Carlson
    Jun 4, 2004
  4. Replies:
    7
    Views:
    833
  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:
    283
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page