Python - Check if two words are in a string
<p>I would like to check whether 2 words "car" and "motorbike" are in each element of an array in Python. I know how to check for one word with <code>in</code> but have no idea how to do with 2 words. Really appreciate any help</p>

<p>I think a simple solution is this:</p>

<pre class="lang-py prettyprint-override"><code>all(map(lambda w: w in text, ('car', 'motorbike')))
</code></pre>

<p>But there might be a problem with this, depending on how picky you need the comparison to be:</p>

<pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; text = 'Can we buy motorbikes in carshops?'
&gt;&gt;&gt; all(map(lambda w: w in text, ('car', 'motorbike')))
True
</code></pre>

<p>The words 'car' and 'motorbike' are NOT in the <code>text</code>, and this still says <code>True</code>. You might need a full match in words. I would do this:</p>

<pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; words = ('car', 'motorbike')
&gt;&gt;&gt; text = 'Can we buy motorbikes in carshops?'
&gt;&gt;&gt; set(words).issubset(text.split())
False
&gt;&gt;&gt; text = 'a car and a motorbike'
&gt;&gt;&gt; set(words).issubset(text.split())
True
</code></pre>

<p>And now it works!</p>

python