【问题标题】:How to check if something is in 2 lists with python如何使用python检查2个列表中是否有东西
【发布时间】:2013-09-04 11:28:35
【问题描述】:

首先,我得说这是聊天机器人的代码。我给机器人一个要跟踪的单词列表,然后我将房间里的所有消息分开。现在我需要做类似的事情:

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

但所有尝试都失败了,这是我的代码。

  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)

理论上应该可行,但是我不完全了解 Sets 的工作原理,我只是用谷歌搜索了我的问题并将我的列表(是的,两者都是列表,而不是 dicts)转换为 Sets,希望能解决问题。尽管如此,我在处理这个问题 1 小时后投降了。

我错过了什么? 感谢您的帮助:)

【问题讨论】:

    标签: python list compare set


    【解决方案1】:

    我认为您需要查看集合之间是否存在intersection

    交叉口(其他,...)

    设置 & 其他 & ...

    返回一个新集合,其中包含集合和所有其他元素共有的元素。

    if s2tracker & s1tracker:
        # do smth
    

    【讨论】:

    • 试过了,现在可以正常工作了……随机,没有错误,但行为很奇怪。我的列表是:[“Bot”、“bot”、“saelyth”、“sael”、“sae”、“sae”、“Sael”、“Sae”、“elyth”] 但是它会忽略我的列表并保存文本每次有人用“a”写一些东西时,例如:这句话a会被保存
    • @Saelyth 可能是因为你只是用空格分割单词..标点符号呢?
    • 呃,你的意思是在 message.body.split() 中?我不确定,如果我用 str(message.body.split()) 打印那个,它会给我这个结果 ['!test', 'this', 'sentence', 'as', 'example'] 所以...我用空格分开应该没关系,对吧?
    • 对,在这个例子中看起来不错。那么,文件中的单词呢?你能打印出来看看有没有问题,比如末尾的换行符?
    • 这就是我在 StackOverflow 中的原因,我也看不出有什么问题 :( 以下是打印结果:prntscr.com/1pa1sm
    【解决方案2】:

    使用内置过滤功能:

    >>> 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
    []
    

    拆分字符串显然会将其转换为单个单词的列表,并且内置的“过滤器”将 lambda 函数作为参数,该函数应该返回 true 或 false 来判断传递给它的项目是否应该包含在结果集。

    更新 - 回答我认为您在评论中提出的问题: 行后:

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

    traceado 应该是一个列表,其中包含与您的单词列表匹配的消息中的单词。本质上,您只需要检查该列表的长度是否为 0:

    if len(trackeado) > 0:
        # Do something
    

    更新更新 - 啊,我刚刚意识到您的缓冲区跟踪器不是一个列表,它只是从文件中读取的一个长字符串。在我的示例中,hot_words 是您要查找的单个单词的列表。根据文件的格式,您需要对其进行处理,将其转换为列表。

    例如如果您的文件是以逗号分隔的单词列表,请执行以下操作:

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

    【讨论】:

    • uhm.... 尝试了这 3 行代码:#1 trackeado = filter(lambda word: word in buffertracker, message.body.split()) #2 if not trackeado == None: #3 print("Test") 但是它保存了所有内容,忽略了我列表中的单词。
    • 等等,我刚刚重新阅读了这篇文章,我应该把 IF true 改为 If not None 吗?
    • 总是得到:
    • 更新更新后,我得到的只是:TypeError: 'filter' 类型的对象没有 len() - 编辑,如果重要的话,我使用的是 3.3.2 python。
    • Ahhhhhhh,对,是的,谢谢,这很重要 :) 在 python 3 中,过滤器函数返回一个生成器,而不是一个列表。您可以通过执行以下操作将您的生成器变成一个列表:list(trackeado)
    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多