wildcard match with list.index()

jeff jeffreykr at gmail.com
Tue Nov 18 18:49:35 EST 2008


On Nov 10, 1:59 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> Mr.SpOOn <mr.spoo... at gmail.com> 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





More information about the Python-list mailing list