tags:

views:

76

answers:

3

Hello, I can do such thing in python:

list = ['one', 'two', 'three']
if 'some word' in list:
   ...

This will check if 'some word' exists in the list. But can I do reverse thing?

list = ['one', 'two', 'three']
if list in 'some one long two phrase three':
    ...

I have to check whether some words from array are in the string. I can do this using cycle but this way has more lines of code.

+9  A: 
if any(word in 'some one long two phrase three' for word in list_):
KennyTM
Python is crazy, thanks.
Ockonal
@Ockonal: and if you want to check that **all** words from that list are inside the string, just replace `any()` above with `all()`
Nas Banov
Note that if 'me' is in `list_`, it will count as a match, since 'me' is in 'some'. If you want to match whole words only, you'll need to change to `any(word in 'some one long two phrase three'.split() for word in list_)`, as I did when creating the sets in my answer.
Paul McGuire
+1  A: 

Here are a couple of alternative ways of doing it, that may be faster or more suitable than KennyTM's answer, depending on the context.

1) use a regular expression:

import re
words_re = re.compile("|".join(list_of_words)

if words_re.search('some one long two phrase three'):
   # do stuff

2) You could use sets if you want to match whole words, e.g. you do not want to find the word "the" in the phrase "them theorems are theoretical":

word_set = set(list_of_words)
phrase_set = set('some one long two phrase three'.split())
if word_set.intersect(phrase_set):
    # do stuff

Of course you can also do whole word matches with regex using the "\b" token.

The performance of these and Kenny's solution are going to depend on several factors, such as how long the word list and phrase string are, and how often they change. If performance is not an issue then go for the simplest, which is probably Kenny's.

Dave Kirby
Thanks for such answer. And, please, add quote after `list_of_words` at second line.
Ockonal
+2  A: 

If your list of words is of substantial length, and you need to do this test many times, it may be worth converting the list to a set and using set intersection to test (with the added benefit that you wil get the actual words that are in both lists):

>>> long_word_list = 'some one long two phrase three about above along after against'
>>> long_word_set = set(long_word_list.split())
>>> set('word along river'.split()) & long_word_set
set(['along'])
Paul McGuire
That won't be the same as it just checks if space separated words match the words you are looking for. You won't be able to find `foo` within `foobar` for example.
poke
@poke - True. It's not clear to me whether the OP wants such partial/embedded word matches or not. As often as not, people write code testing for a word within a larger string of words, assuming they are doing word matching but in fact are doing string matching. This method checks whole words against a set of whole words, without looking for any embedded matches (such as matching 'out' in 'about').
Paul McGuire
Yeah sure, I just thought it might be important to mention that your solution (which is a good one btw.) does not behave the same as the `in` operator.
poke