Python:检查字典项是否在字符串中,然后返回匹配项

时间:2016-11-29 10:27:55

标签: python dictionary

我有一个类似于以下内容的数据库表:

id    phrases                    message
1     "social media, marketing"  "This person is in marketing!"
2     "finance, fintech          "This person is in finance!"

我已经完成了关键短语并将它们添加到字典中,如下所示:

messages = self.db_access.execute("SELECT * FROM messages")
messages = self.db_access.fetchall()
print(len(messages))
if self.db_access.rowcount > 0:
    has_message = True
    phrase_list = {}
    for the_rule in messages:
        print(the_rule[1])
        rule = the_rule[1].split(',')
        for phrase in rule:
            phrase = str(phrase)
            phrase_list[phrase] = str(the_rule[2])
    print(phrase_list)
    print("\n")
else:
    has_message = False

然后产生以下内容:

# phrase_list
{'social media': 'This person is in marketing!', 
 'marketing': 'This person is in marketing!', 
 'finance': 'This person is in finance!', 
 'fintech': 'This person is in finance!'}

因此,每个短语都有自己的伴随消息,在其他地方使用。

现在,我可以将这些dict键与字符串进行比较,如下所示:

descriptions = ["I am in marketing, and it is super awesome", "I am in finance, and it is super awesome"]

我的下一步是将该字符串与键进行比较,如果它包含任何关键字,则打印匹配的键及其值/消息。这就是我到目前为止所做的:

for description in descriptions:
    print(description)
    if has_message == True:
        if any(x in description for x in phrase_list):
            # print matching keyword and message
        else:
            print("No matches, but a phrase list exists")

所以我的问题是,我需要用什么来替换输出1)与其匹配的关键字,以及2)与该关键字相关联的消息?

2 个答案:

答案 0 :(得分:1)

您只需稍微重新构建代码即可。使用any时不需要返回x使表达式评估为True的信息引起的需求。它只是告诉你某人做了或没人做了。如果你关心你必须循环或可能使用next。无论如何,这是一种方法:

for description in descriptions:
    print(description)
    if has_message == True:
        for x in phrase_list:
            if x in description:
                print(x, phrase_list[x])
                break
        else:
            print("No matches, but a phrase list exists")

注意:

如果else上的for令人困惑,请将其删除。只有当x不在任何描述中时,代码才会到达。

答案 1 :(得分:1)

可能想稍微调整一下,但你可以使用正则表达式来搜索匹配的键,然后在你的字典中查找,例如:

import re

phrase_list = {'social media': 'This person is in marketing!', 
 'marketing': 'This person is in marketing!', 
 'finance': 'This person is in finance!', 
 'fintech': 'This person is in finance!'}

descriptions = ["I am in marketing, and it is super awesome", "I am in finance, and it is super awesome", 'john smith']

def find_department(lookup, text):
    m = re.search('|'.join(sorted(lookup, key=len, reverse=True)), text)
    if m:
        return lookup[m.group(0)]
    else:
        return 'This person is a mystery!'

然后运行它会给你:

for desc in descriptions:
    print(desc, '->', find_department(phrase_list, desc))

#I am in marketing, and it is super awesome -> This person is in marketing!
#I am in finance, and it is super awesome -> This person is in finance!
#john smith -> This person is a mystery!