By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

wildcard match with list.index()

P: n/a
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.
Nov 10 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 10.11.2008, Mr.SpOOn <mr********@gmail.comwroted:
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 guess there's a way to use the glob module in this situation, but
for the specific case I think you can use:

start_with_no = (i for i in mylist if i.startswith("no"))

GS
--
Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
Nocturnal Infiltration and Accurate Killing
Nov 10 '08 #2

P: n/a
Mr.SpOOn <mr********@gmail.comwrites:
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
Nov 10 '08 #3

P: n/a
Thanks, I just have to choose which one to use :)
Nov 11 '08 #4

P: n/a
On Nov 10, 1:59 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
Mr.SpOOn <mr.spoo...@gmail.comwrites:
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
Nov 18 '08 #5

P: n/a
jeff <je*******@gmail.comwrote:
>>>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:

pylst.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:

pydct = dict((x[0], x[1:]) for x in lst)
pydct['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 -- si***@chiark.greenend.org.uk -- 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
Nov 19 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.