Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > in operator for strings

Reply
Thread Tools

in operator for strings

 
 
Moosebumps
Guest
Posts: n/a
 
      05-25-2004
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'


 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      05-25-2004
"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>




 
Reply With Quote
 
 
 
 
Heather Coppersmith
Guest
Posts: n/a
 
      05-25-2004
On Tue, 25 May 2004 08:56:19 GMT,
"Moosebumps" <(E-Mail Removed)> 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
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      05-25-2004
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" <(E-Mail Removed)> wrote in message
news:TSDsc.72714$(E-Mail Removed). 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'
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments