473,508 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

in operator for strings

Wouldn't it be cool if you could do this?

('hello','test') in 'blah blah hello blah test':

and it would say if all elements of the list are in the string?

it would be more efficient and more readable than:

'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
test'
Jul 18 '05 #1
3 1133
On Tue, 25 May 2004 08:56:19 GMT,
"Moosebumps" <mo********@moosebumps.mb> wrote:
Wouldn't it be cool if you could do this?
('hello','test') in 'blah blah hello blah test': and it would say if all elements of the list are in the string? it would be more efficient and more readable than: 'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
test'


Untested:

def string_contains_any_of( targets, the_string ):
for target in targets:
if target in the_string:
return True
return False

string_contains_any_of( ('hello', 'test'), 'blah blah blah' )

HTH,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
Jul 18 '05 #2
"Moosebumps" wrote:
Wouldn't it be cool if you could do this?

('hello','test') in 'blah blah hello blah test':

and it would say if all elements of the list are in the string?


well, if you're asking me, I'd say that what's really cool is that you
can do not only

findall(('hello', 'test'), 'blah blah hello blah test')

but also

find_any(('hello', 'test'), 'blah blah hello blah test')

find_in_order(('hello', 'test'), 'blah blah hello blah test')

find_in_any_case(('hello', 'test'), 'blah blah hello blah test')

starts_and_ends_with(('hello', 'test'), 'blah blah hello blah test')

find_only_on_fridays(('hello', 'test'), 'blah blah hello blah test')

etc.

without having to change the language, or any of its implementations.

</F>


Jul 18 '05 #3
This can be written as:

a=('hello','test')
b='blah blah hello blah test'
reduce(lambda x,y: x and b.count(y), a)

or as a fuction:

def contains_all(a, b):
return reduce(lambda x,y: x and b.count(y), a)
Then in main program

if contains_all(a,b):
...

Larry Bates
Syscon, Inc.

"Moosebumps" <mo********@moosebumps.mb> wrote in message
news:TS*******************@newssvr25.news.prodigy. com...
Wouldn't it be cool if you could do this?

('hello','test') in 'blah blah hello blah test':

and it would say if all elements of the list are in the string?

it would be more efficient and more readable than:

'hello' in 'blah blah hello blah test' and 'test' in 'blah blah hello blah
test'

Jul 18 '05 #4

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

Similar topics

6
2352
by: Ruchika | last post by:
Hi, I am new to Python, so this may be a very simple question for most of you. What does the % operator stand for in Python? I came across a script that uses the % operator as follows - def...
3
4429
by: Prakash Bande | last post by:
Hi, I have bool operator == (xx* obj, const string st). I have declared it as friend of class xx. I am now able to do this: xx ox; string st; if (&ox == st) { } But, when I have a vector<xx*>...
4
2222
by: Matthias | last post by:
Now I am confused. I am doing a "less" comparison in my program between strings, to see which strings in some range precede others. This is my approach: class SortedByName { public: bool...
3
17072
by: jl_post | last post by:
Hi, I recently wrote two benchmark programs that compared if two strings were equal: one was a C program that used C char arrays with strcmp(), and the other was a C++ program that used...
17
2480
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
17
1899
by: Jedrzej Miadowicz | last post by:
I recently (re)discovered data binding in Windows Forms thanks to its advances in Visual Studio 2005. As I looked a little deeper, however, I realize that it still suffers from an irksome tendency...
17
2008
by: ma740988 | last post by:
Consider: # include <iostream> # include <algorithm> # include <vector> # include <string> using namespace std; class msg { std::string someStr;
12
2190
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
8
1677
by: imutate | last post by:
How do you do assignment operator within a template ? #include <vector> template < typename T > class Vec : public std::vector< T { public: Vec() { } Vec( int s ) : std::vector<T>(s) { }...
1
2684
by: buburuz | last post by:
Hi, I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an...
0
7323
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,...
0
7380
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...
1
7039
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...
0
5626
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,...
1
5050
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...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.