473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

re.search over a list

Pat
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!
Oct 19 '08 #1
5 1133
Pat
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"^so mestring$", "\n".join(words ), re.MULTILINE)

>I love succinct but clearly understandable code.

separator.join( sequence_of_str ings) 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!
Oct 20 '08 #2
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"^s omestring$", "\n".join(words ), re.MULTILINE)

>>I love succinct but clearly understandable code.

separator.join (sequence_of_st rings) 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 !-)
Oct 20 '08 #3
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"^s omestring$", "\n".join(words ), re.MULTILINE)

>>I love succinct but clearly understandable code.

separator.join (sequence_of_st rings) 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(pa t, 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/

Oct 23 '08 #4
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(pa t, 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.
Oct 23 '08 #5
Pat
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.joi n(sequence_of_s trings) 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.
Oct 23 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
13554
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., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it...
0
1545
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 reason is just for fun). Please let me know if you need this utility. Thanks Sushil
1
7105
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
10
9758
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;
3
1770
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 string arrays, which are just lists of people or companies in alphabetic order. These names may have accented and umlauted characters (which are...
12
9779
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. That event procedure should clear the text boxes that are used for a search query that loads the results into a list box on that same form. When...
0
2066
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 end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently...
5
1958
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 over a period of time. There can be quite similar assets and simply choosing from the combo box is not enough. I need something more...
13
4969
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 search, taking it's criteria fro a text box on a form. This bit is easy, done it a jazillion times. But I want it to be an "Or" search. If I type in it...
11
5579
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 can enter their search criteria eg. surname, reg num, etc. in the text boxes. The multi select list box allows the user to select multiple counties...
0
7457
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...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7965
jinu1996
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...
1
7483
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6051
agi2029
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...
1
5375
isladogs
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...
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.