Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you only want to know if <em>any</em> item of <code>d</code> is contained in <code>paid[j]</code>, as you literally say:</p> <pre><code>if any(x in paid[j] for x in d): ... </code></pre> <p>If you also want to know <em>which</em> items of <code>d</code> are contained in <code>paid[j]</code>:</p> <pre><code>contained = [x for x in d if x in paid[j]] </code></pre> <p><code>contained</code> will be an empty list if no items of <code>d</code> are contained in <code>paid[j]</code>.</p> <p>There are other solutions yet if what you want is yet another alternative, e.g., get the <em>first</em> item of <code>d</code> contained in <code>paid[j]</code> (and <code>None</code> if no item is so contained):</p> <pre><code>firstone = next((x for x in d if x in paid[j]), None) </code></pre> <p>BTW, since in a comment you mention sentences and words, maybe you don't necessarily want a <em>string</em> check (which is what all of my examples are doing), because they can't consider word boundaries -- e.g., each example will say that 'cat' is <code>in</code> 'obfuscate' (because, 'obfuscate' contains 'cat' <em>as a substring</em>). To allow checks on word boundaries, rather than simple substring checks, you might productively use regular expressions... but I suggest you open a separate question on that, if that's what you require -- all of the code snippets in this answer, depending on your exact requirements, will work equally well if you change the predicate <code>x in paid[j]</code> into some more sophisticated predicate such as <code>somere.search(paid[j])</code> for an appropriate RE object <code>somere</code>. (Python 2.6 or better -- slight differences in 2.5 and earlier).</p> <p>If your intention is something else again, such as getting one or all of the <em>indices</em> in <code>d</code> of the items satisfying your constrain, there are easy solutions for those different problems, too... but, if what you actually require is so far away from what you said, I'd better stop guessing and hope you clarify;-).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload