Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Now that you've fixed the original problem, and fixed the next problem with doing the check backward, and renamed all of your variables, you have this:</p> <pre><code>for match in dictionary: if any(match in value for value in sentences): print match </code></pre> <p>And your problem with it is:</p> <blockquote> <p>The way I have the code written i can get the dictionary items but instead i want to print the sentences. </p> </blockquote> <p>Well, yes, your <code>match</code> is a dictionary item, and that's what you're printing, so of course that's what you get.</p> <p>If you want to print the sentences that contain the dictionary item, you can't use <a href="http://docs.python.org/3/library/functions.html#any" rel="nofollow"><code>any</code></a>, because the whole point of that function us to just return True if any elements are true. It won't tell you which ones—in fact, if there are more than one, it'll stop at the first one.</p> <p>If you don't understand functions like <code>any</code> and the generator expressions you're passing to them, you really shouldn't be using them as magic invocations. Figure out how to write them as explicit loops, and you will be able to answer these problems for yourself easily. (Note that the <code>any</code> docs directly show you how to write an equivalent loop.)</p> <p>For example, your existing code is equivalent to:</p> <pre><code>for match in dictionary: for value in sentences: if match in value: print match break </code></pre> <p>Written that way, it should be obvious how to fix it. First, you want to print the sentence instead of the word, so print <code>value</code> instead of <code>match</code> (and again, it would really help if you used meaningful variable names like <code>sentence</code> and <code>word</code> instead of meaningless names like <code>value</code> and misleading names like <code>match</code>…). Second, you want to print all matching sentences, not just the first one, so don't <code>break</code>. So:</p> <pre><code>for match in dictionary: for value in sentences: if match in value: print value </code></pre> <p>And if you go back to my first answer, you may notice that this is the exact same structure I suggested.</p> <p>You <em>can</em> simplify or shorten this by using comprehensions and iterator functions, but not until you understand the simple version, and how those comprehensions and iterator functions work.</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