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. 5 18532
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
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
Thanks, I just have to choose which one to use :)
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: questions? |
last post by:
I want to do list index function.
>>> y=
>>> y
>>> y.index
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsubscriptable object
It works with y=. Anyone has...
|
by: William Meyer |
last post by:
hi,
I need to get the index of an object in a list. I know that no two objects
in the list are the same, but objects might evaluate as equal. for example
list =
for object in list:...
|
by: sam |
last post by:
hey everybody, this is my first time posting here. i'm pretty new to
python and programming in general (as you'll soon work out for
yourselves...)
i'm trying to code a version of a selection...
|
by: erikwickstrom |
last post by:
Hi all,
I'm sorry about the newbie question, but I've been searching all
afternoon and can't find the answer!
I'm trying to get this bit of code to work without triggering the
IndexError.
...
|
by: zzbbaadd |
last post by:
What's with the index() function of lists throwing an exception on not
found? Let's hope this is rectified in Python 3. If nothing else, add
a function that doesn't throw an exception. There are a...
|
by: python101 |
last post by:
My code
s=
for k in range(len(s)):
st=s.count(s)
if st>=2:
print s.pop(s.index(s))
Traceback (most recent call last):
|
by: Riccardo Murri |
last post by:
Hello,
I have some code that stops when trying to find a graph in a list of
similar graphs::
(Pydb) list
110 try:
111 canonical = self.base
112 except ValueError:
113 ...
|
by: Georgy Panterov |
last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range."
The...
|
by: Chriskim |
last post by:
I wrote a program that uses PyQt4, mathplotlib to
generates graphs. I made a executable file using Py2exe.
Everything is working fine, but when I exit from my
program, a window message pops...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |