Python:检查字典项是否在字符串中,然后返回匹配项
我有一个如下所示的数据库表:Python:检查字典项是否在字符串中,然后返回匹配项,python,dictionary,Python,Dictionary,我有一个如下所示的数据库表: id phrases message 1 "social media, marketing" "This person is in marketing!" 2 "finance, fintech "This person is in finance!" # phrase_list {'social media': 'This person is in marketing!', 'm
id phrases message
1 "social media, marketing" "This person is in marketing!"
2 "finance, fintech "This person is in finance!"
# 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!'}
我循环浏览了关键短语并将其添加到字典中,如下所示:
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
descriptions = ["I am in marketing, and it is super awesome", "I am in finance, and it is super awesome"]
这将产生以下结果:
id phrases message
1 "social media, marketing" "This person is in marketing!"
2 "finance, fintech "This person is in finance!"
# 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键与字符串进行比较,如下所示:
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
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)与该关键字关联的消息?您只需稍微重新构造代码。需要这样做是因为使用了
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
令人困惑,只需将其删除即可。仅当x不在任何描述中时,代码才会到达它。您只需稍微重新构造代码即可。需要这样做是因为使用了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
令人困惑,只需将其删除即可。仅当x不在任何描述中时,代码才会到达它。可能需要对其进行一些调整,但您可以使用正则表达式搜索匹配的键,然后在字典中查找该键,例如:
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!
可能需要对其进行一些调整,但您可以使用正则表达式搜索匹配的键,然后在字典中查找该键,例如:
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!
因此,您需要查看descriptions
列表中的字符串是否包含任何作为dict?@Ev.Kounis上的值存储的字符串。OP希望测试说明
列表中的每个字符串,以查看它是否包含在短语列表
中存储为键的任何字符串,如果是,则打印键及其相关值。如果所有键都是单字,这将很容易,但像“社交媒体”这样的键会让它变得有点棘手。@PM2Ring-ExactlySo您想要的是查看descriptions
列表中的字符串是否包含任何存储为dict?@Ev.Kounis上的值的字符串。OP希望测试说明
列表中的每个字符串,以查看它是否包含在短语列表
中存储为键的任何字符串,如果是,则打印键及其相关值。如果所有的键都是单字,这将很容易,但像“社交媒体”这样的键会让它变得有点棘手。@PM2Ring-这正是我需要的,谢谢。它完全符合我现有的代码,不需要更多的函数或导入的库!这正是我需要的,谢谢。它完全符合我现有的代码,不需要更多的函数或导入的库!