Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the built-in filter function:</p> <pre><code>&gt;&gt;&gt; hot_words = ["spam", "eggs"] &gt;&gt;&gt; message_body = "Oh boy, my favourite! spam, spam, spam, eggs and spam" &gt;&gt;&gt; matching_words = filter(lambda word: word in hot_words, message_body.split()) &gt;&gt;&gt; matching_words ['eggs', 'spam'] &gt;&gt;&gt; message_body = "No, I'd rather just egg and bacon" &gt;&gt;&gt; matching_words = filter(lambda word: word in hot_words, message_body.split()) &gt;&gt;&gt; matching_words [] </code></pre> <p>Splitting the string obviously turns it into a list of individual words, and the built-in 'filter' takes a lambda function as an argument which should returns true or false as to whether the item passed to it, should be included in the result set.</p> <p><strong>Update</strong> - To answer the question that I <em>think</em> you're asking in your comment: After the line:</p> <pre><code>trackeado = filter(lambda word: word in buffertracker, message.body.split()) </code></pre> <p>traceado should be a list containing the words in a message that match your list of words. Essentially, you just need to check whether or not that list has a 0 length or not:</p> <pre><code>if len(trackeado) &gt; 0: # Do something </code></pre> <p><strong>Update update</strong> - Ah, I've just realised that your buffertracker isn't a list, it's just a long string read in from a file. In my example, hot_words is a list of individual words that you're looking for. Depending on how your file is formatted, you'll need to do something to it, to turn it into a list. </p> <p>e.g. if your file is a comma-separated list of words, do this:</p> <pre><code>&gt;&gt;&gt; words = buffer tracker.split(',') &gt;&gt;&gt; trackeado = filter(lambda word: word in words, message.body.split()) &gt;&gt;&gt; if len(trackeado) &gt; 0: ... print "found" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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