Saelyth
Saelyth

Reputation: 1734

How to check if something is in 2 lists with python

First of all, i gotta say this is the code for a chat bot. I am giving the bot a list of words to track and then i'm splitting all messages in the room. Now i need to make something like:

IF any word from my list is IN message.body THEN do something. 

But all attemps failed, this is my code.

  leyendotracker = open("listas\eltracker.txt", "r") #Open file with tracker words
  buffertracker = leyendotracker.read() #Read words and save them in a variable
  leyendotracker.close() #Close file
  s1tracker = set(message.body.split()) #Set the messages in chat as a Set
  s2tracker = set(buffertracker) #Set the variable with words from file as a Set
  if s2tracker in s1tracker: #Check if any word from the file is in the message from chat.
    print("[TRACKER - "+user.name+" said: "+message.body)

That should work in theory, however i don't fully understand how Sets works and i just googled my problem and converted my lists (yes, both are lists, not dicts) into Sets hoping that would fix the problem. Nevertheless, i surrender after 1 hour of dealing with this problem.

What am i missing? Thanks for help :)

Upvotes: 0

Views: 249

Answers (2)

David Downes
David Downes

Reputation: 1205

Use the built-in filter function:

>>> hot_words = ["spam", "eggs"]
>>> message_body = "Oh boy, my favourite! spam, spam, spam, eggs and spam"
>>> matching_words = filter(lambda word: word in hot_words, message_body.split())
>>> matching_words
['eggs', 'spam']
>>> message_body = "No, I'd rather just egg and bacon"
>>> matching_words = filter(lambda word: word in hot_words, message_body.split())
>>> matching_words
[]

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.

Update - To answer the question that I think you're asking in your comment: After the line:

trackeado = filter(lambda word: word in buffertracker, message.body.split())

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:

if len(trackeado) > 0:
    # Do something

Update update - 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.

e.g. if your file is a comma-separated list of words, do this:

>>> words = buffer tracker.split(',')
>>> trackeado = filter(lambda word: word in words, message.body.split())
>>> if len(trackeado) > 0:
...     print "found"

Upvotes: 1

alecxe
alecxe

Reputation: 473833

I think you need to see if there is an intersection between sets:

intersection(other, ...)

set & other & ...

Return a new set with elements common to the set and all others.

if s2tracker & s1tracker:
    # do smth

Upvotes: 3

Related Questions