简体   繁体   English

如何使用python检查2个列表中是否包含某些内容

[英]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. 从理论上讲应该可以,但是我不完全了解Sets的工作原理,我只是用谷歌搜索问题并将列表(是的,都是列表,而不是字典)转换为Sets,希望可以解决问题。 Nevertheless, i surrender after 1 hour of dealing with this problem. 不过,在处理了这个问题1小时后,我投降了。

What am i missing? 我想念什么? Thanks for help :) 感谢帮助 :)

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

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. 拆分字符串显然会将其变成单个单词的列表,并且内置的“过滤器”将lambda函数作为参数,对于传递给它的项目是否应包含在结果集中,应返回true或false 。

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. traceado应该是包含消息中与您的单词列表匹配的单词的列表。 Essentially, you just need to check whether or not that list has a 0 length or not: 本质上,您只需要检查该列表的长度是否为0:

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. 更新更新 -嗯,我刚刚意识到您的buffertracker不是列表,而是从文件中读取的一个长字符串。 In my example, hot_words is a list of individual words that you're looking for. 在我的示例中,hot_words是您要查找的单个单词的列表。 Depending on how your file is formatted, you'll need to do something to it, to turn it into a list. 根据文件的格式,您需要对其进行处理以将其转换为列表。

eg 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM