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('n o*')
Of course this doesn't work. 5 18852
On 10.11.2008, Mr.SpOOn <mr********@gma il.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('n o*')
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("n o"))
GS
--
Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
Nocturnal Infiltration and Accurate Killing
Mr.SpOOn <mr********@gma il.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('n o*')
Of course this doesn't work.
I have exactly what you need :)
>>import fnmatch fnmatch.filte r(['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...@google mail.comwrote:
Mr.SpOOn <mr.spoo...@gma il.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('n o*')
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*******@gmai l.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.gr eenend.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 any hint, what's the reason
|
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:
objectIndex = list.index(object)
print objectIndex
|
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 sort and the heart of the
code is as follows (no_lines is simply the number of items to be
sorted, read out of an input file):
for j in range(0, no_lines):
|
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.
import shutil, os, sys
|
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 million
situations where you can have an item not be in a list and it is not
an exception situation.
| |
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 raise ValueError, \
|
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 strange thing is that it will sometimes run for 10 iterations sometimesfor only a few and sometimes won't run at all (seemingly arbitrary).
Here is some of the code:
for _ in range(100):
handA=deal_hand(DECK) #deals 2 'hole' cards from a DECK...
|
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 out and tells me to look
at the log file. In the log file, here is what they said
C:\Users\dmckinnon\Desktop\ChrisKim\eSimulator_original\src\dist\library.zip\matplotlib\__init__.py:572: UserWarning: Could not find matplotlibrc; using defaults...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |