While I can use a for loop looking for a match on a list, I was
wondering if there was a one-liner way.
In particular, one of my RE's looks like this '^somestring$' so I can't
just do this: re.search( '^somestring$', str( mylist ) )
I'm not smart enough (total newbie) to code up a generator expression
and I was wondering if I'm missing something obvious.
I love succinct but clearly understandable code.
thx! 5 1105
Bruno Desthuilliers wrote:
Pat a écrit :
>While I can use a for loop looking for a match on a list, I was wondering if there was a one-liner way.
In particular, one of my RE's looks like this '^somestring$' so I can't just do this: re.search( '^somestring$', str( mylist ) )
I'm not smart enough (total newbie) to code up a generator expression and I was wondering if I'm missing something obvious.
words = ['foo', 'bar', 'somestring', 'baaz']
re.search(r"^somestring$", "\n".join(words), re.MULTILINE)
>I love succinct but clearly understandable code.
separator.join(sequence_of_strings) is a very common python idiom, so
you can consider it as readable.
That's excellent. Exactly what I was looking for.
Thank you VERY much!
Pat a écrit :
Bruno Desthuilliers wrote:
>Pat a écrit :
>>While I can use a for loop looking for a match on a list, I was wondering if there was a one-liner way.
In particular, one of my RE's looks like this '^somestring$' so I can't just do this: re.search( '^somestring$', str( mylist ) )
I'm not smart enough (total newbie) to code up a generator expression and I was wondering if I'm missing something obvious.
words = ['foo', 'bar', 'somestring', 'baaz'] re.search(r"^somestring$", "\n".join(words), re.MULTILINE)
>>I love succinct but clearly understandable code.
separator.join(sequence_of_strings) is a very common python idiom, so you can consider it as readable.
That's excellent. Exactly what I was looking for.
Thank you VERY much!
Note that at least for this exact case, you don't need re at all:
>>'somestring' in words
True
But I guess you do have some less trivial use case !-)
Pat wrote:
Bruno Desthuilliers wrote:
>Pat a écrit :
>>While I can use a for loop looking for a match on a list, I was wondering if there was a one-liner way.
In particular, one of my RE's looks like this '^somestring$' so I can't just do this: re.search( '^somestring$', str( mylist ) )
I'm not smart enough (total newbie) to code up a generator expression and I was wondering if I'm missing something obvious.
words = ['foo', 'bar', 'somestring', 'baaz'] re.search(r"^somestring$", "\n".join(words), re.MULTILINE)
>>I love succinct but clearly understandable code.
separator.join(sequence_of_strings) is a very common python idiom, so you can consider it as readable.
That's excellent. Exactly what I was looking for.
I suspect that
any(re.match(pat, word) for word in words)
might be a more efficient way to do this.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Steve Holden a écrit :
Pat wrote:
>Bruno Desthuilliers wrote:
(snip)
>>words = ['foo', 'bar', 'somestring', 'baaz'] re.search(r"^somestring$", "\n".join(words), re.MULTILINE)
(snip)
>>
I suspect that
any(re.match(pat, word) for word in words)
might be a more efficient way to do this.
Indeed. I'm not yet used to have all and any builtins, thanks for the
reminder.
Bruno Desthuilliers wrote:
Pat a écrit :
>Bruno Desthuilliers wrote:
>>Pat a écrit : While I can use a for loop looking for a match on a list, I was wondering if there was a one-liner way.
In particular, one of my RE's looks like this '^somestring$' so I can't just do this: re.search( '^somestring$', str( mylist ) )
I'm not smart enough (total newbie) to code up a generator expression and I was wondering if I'm missing something obvious.
words = ['foo', 'bar', 'somestring', 'baaz'] re.search(r"^somestring$", "\n".join(words), re.MULTILINE)
I love succinct but clearly understandable code.
separator.join(sequence_of_strings) is a very common python idiom, so you can consider it as readable. That's excellent. Exactly what I was looking for.
Thank you VERY much!
Note that at least for this exact case, you don't need re at all:
>>'somestring' in words
True
But I guess you do have some less trivial use case !-)
I used re because I wanted a string that was not a substring, hence the
^$. In my trivial example, I used words but my intent was to search for
words within a longer string. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: RAJASEKHAR KONDABALA |
last post by:
Hi,
Does anybody know what the fastest way is to "search for a value in a
singly-linked list from its tail" as oposed to its head?
I am talking about a non-circular singly-linked list, i.e.,...
|
by: Sushil Srivastava |
last post by:
Hi Guys,
I have developed a utility that provide a smart MSDN
knowledge base search both off-line and on-line. I have
describe more in detail below why I decided to write this
utility (one...
|
by: cyrvb |
last post by:
Hello,
I'm a very very newbie in C# I did start 2 days ago, I get Visual
Stuido 2005 C#
I try to understand how to manage the arrays
I did write this
|
by: free2cric |
last post by:
Hi,
I have a single link list which is sorted.
structure of which is like
typedef struct mylist
{
int num;
struct mylist *next;
|
by: Harry Haller |
last post by:
What is the fastest way to search a client-side database?
I have about 60-65 kb of data downloaded to the client which is
present in 3 dynamically created list boxes. The boxes are filled from
3...
|
by: ljungers |
last post by:
I'm on the home streach of my project and found that my "Reset for New Search" command button not working as desired. What should happen is that when the button is clicked a Event Procedure is run....
|
by: |
last post by:
I have a question about spawning and displaying subordinate list controls
within a list control. I'm also interested in feedback about the design of
my search application. Lots of code is at the...
|
by: Deano |
last post by:
Perhaps this has been asked before but there might be some up to date
thinking about this.
I really need a better search function for my asset register. I allow
assets to be entered and tracked...
|
by: Robertf987 |
last post by:
Hi,
Yet another thing I need help with I'm affraid. I'll first explain what I want, then I'll try to explain what I have. I'm using Microsoft Access 2000.
What I want is to be able to do a...
|
by: woodey2002 |
last post by:
This problem is driving me crazy.
Hello there,
i am trying to create a search form for records in my access database. The search form will contain text boxes and a multi select list box. The user...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |