wildcard match with list.index()

M

Mr.SpOOn

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.
 
A

Arnaud Delobelle

Mr.SpOOn said:
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
 
J

jeff

Mr.SpOOn said:
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:

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

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 said:
1
 
S

Sion Arrowsmith

jeff said:
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 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top