gpt4 book ai didi

python - 如何在Python中将2列表中的单词与另一个单词字符串进行匹配而无需子字符串匹配?

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:35 25 4
gpt4 key购买 nike

我有 2 个包含关键字的列表:

slangNames = [Vikes, Demmies, D, MS Contin]
riskNames = [enough, pop, final, stress, trade]

我还有一个名为 overallDict 的字典,其中包含推文。键值对是{ID:推文文本)例如:

{1:"Vikes is not enough for me", 2:"Demmies is okay", 3:"pop a D"}

我试图仅从 slangNames 和riskNames 中分离出那些至少具有一个关键字的推文。因此,推文必须包含 slangNames 中的任何关键字以及riskNames 中的任何关键字。因此,从上面的示例来看,我的代码应该返回键 1 和 3,即

{1:"Vikes is not enough for me", 3:"pop a D"}. 

但是我的代码正在拾取子字符串而不是完整的单词。所以基本上,任何带有字母“D”的东西都会被选中。如何将它们匹配为整个单词而不是子字符串?请帮忙。谢谢!

到目前为止我的代码如下:

for key in overallDict:
if any(x in overallDict[key] for x in strippedRisks) and (any(x in overallDict[key] for x in strippedSlangs)):
output.append(key)

最佳答案

将 slangNames 和riskNames存储为集合,拆分字符串并检查是否有任何单词出现在两个集合中

slangNames = set(["Vikes", "Demmies", "D", "MS", "Contin"])
riskNames = set(["enough", "pop", "final", "stress", "trade"])
d = {1: "Vikes is not enough for me", 2:"Demmies is okay", 3:"pop a D"}

for k,v in d.items():
spl = v.split() # split once
if any(word in slangNames for word in spl) and any(word in riskNames for word in spl):
print(k,v)

输出:

1 Vikes is not enough for me
3 pop a D

或者不使用set.isdisjoint:

slangNames = set(["Vikes", "Demmies", "D", "MS", "Contin"])
riskNames = set(["enough", "pop", "final", "stress", "trade"])
d = {1: "Vikes is not enough for me", 2:"Demmies is okay", 3:"pop a D"}

for k,v in d.items():
spl = v.split()
if not slangNames.isdisjoint(spl) and not riskNames.isdisjoint(spl):
print(k, v)

使用any应该是最有效的,因为我们会在第一场比赛中短路。如果两个集合的交集是空集,则它们是不相交的,因此如果 if not slangNames.isdisjoint(spl) 为 True,则至少会出现一个常见单词。

如果 MS Contin 实际上是一个单词,您还需要捕获这一点:

import re
slangNames = set(["Vikes", "Demmies", "D"])
r = re.compile(r"\bMS Contin\b")
riskNames = set(["enough", "pop", "final", "stress", "trade"])
d = {1: "Vikes is not enough for me", 2:"Demmies is okay", 3:"pop a D"}

for k,v in d.items():
spl = v.split()
if (not slangNames.isdisjoint(spl) or r.search(v)) and not riskNames.isdisjoint(spl):
print(k,v)

关于python - 如何在Python中将2列表中的单词与另一个单词字符串进行匹配而无需子字符串匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com