寻找两个集合/字典的交集以及它们在原始集合/字典中各自的出现

2024-10-04 07:39:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在两个不同的集合上建立一个分数,包含三叉树,使用它们的出现。数据是这样的。两者都是用集合中的Counter创建的dict。你知道吗

cntTrigramsPos.most_common(3)
Out[125]:

[(('This', 'is', 'a'), 7),
 (('I', 'love', 'this'), 6),
 (('I', 'am', 'very'), 5)]

cntTrigramsNeg.most_common(3)
Out[150]:
[(('I', 'had', 'to'), 6),
 (('work', 'with', 'my'), 4),
 (('I', 'could', 'not'), 4)]

我想用这两条格言来计算分数。Nr是dicts中的事件。你知道吗

score = (nrPos - nrNeg) / (nrPos + nrNeg)

我把它们变成了一组。我可以这样找到两个集合的交集。你知道吗

trigramsPosSet = set(cntTrigramsPos)
trigramsNegSet = set(cntTrigramsNeg)

for i in trigramsPosSet.intersection(trigramsNegSet):
print (i, cntTrigramsNeg[i])

这样,我只知道交叉点,而不知道原始录音中的出现,我需要用它来计算分数。我要做的是比较两个dict中的相同键,并提取具有交集的出现(值)。在上面,你可以看到交叉点,但看不到发生在dicts。我用哪种方法能再弄一次?我希望能够使用我的公式连接交叉点和计算出的分数。谢谢。你知道吗


Tags: 数据mostcommonout分数dictsetdicts