Python search exact word from list in string?
<p>I need to find exact word from list in the string.</p>

<p>I tried below code. Here I am getting exact match for single word from list but how to match two words from the list.</p>

<pre><code>categories_to_retain = 
['SOLID',
 'GEOMETRIC',
 'FLORAL',
 'BOTANICAL',
 'STRIPES',
 'ABSTRACT',
 'ANIMAL',
 'GRAPHIC PRINT',
 'ORIENTAL',
 'DAMASK',
 'TEXT',
 'CHEVRON',
 'PLAID',
 'PAISLEY',
 'SPORTS']

x = " Beautiful Art By  Design Studio **graphic print** Creates A **TEXT** Design For This Art Driven Duvet. Printed In Remarkable Detail On A Woven Duvet, This Is An Instant Focal Point Of Any Bedroom. The Fabric Is Woven Of Easy Care Polyester And Backed With A Soft Poly/Cotton Blend Fabric. The Texture In The Fabric Gives Dimension And A Unique Look And Feel To The Duvet."

x = x.upper()

print x

#x = "GRAPHIC"
#x = "GRAPHIC PRINTS"


matches = [cat for cat in categories_to_retain if cat in x.split()]

matches

Output:
['TEXT']
</code></pre>

<p>Here you can see there is word present in my list called 'GRAPHIC PRINT'. I want find this word from my string.</p>

<p>Also i need to find word even if its present in plural or past tense. For example,STRIPED,STRIPE,GRAPHIC PRINTS etc.</p>

<p>Thanks ,
Niranjan</p>

<p>Here you are splitting the string using default split(), which means it will be split at each space: there will be the strings "GRAPHIC" and "PRINT" in x.split(), but not "GRAPHIC  PRINT". You may want to use "if cat in x", which I believe would return what you need in this case.</p>

<p>This should work:</p>

<pre><code>matches = [cat for cat in categories_to_retain if cat in x]
</code></pre>

python regex list search find