Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找两个集合/词典的交集及其在原始集合/词典中的各自出现处_Python_Dictionary_Set_Intersection - Fatal编程技术网

Python 查找两个集合/词典的交集及其在原始集合/词典中的各自出现处

Python 查找两个集合/词典的交集及其在原始集合/词典中的各自出现处,python,dictionary,set,intersection,Python,Dictionary,Set,Intersection,我试图用它们的发生率在两个不同的集合上建立一个分数,包含三叉图。数据如下所示。两者都是使用集合中的计数器创建的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),

我试图用它们的发生率在两个不同的集合上建立一个分数,包含三叉图。数据如下所示。两者都是使用集合中的计数器创建的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中的事件。我用哪种方法能再弄一次?我希望能够使用我的公式将交点与计算出的分数连接起来。非常感谢。

你能澄清一下dicts中出现的
是什么意思吗-
cntrigramseng[i]
cntrigramspos[i]
应该给你你所需要的我做了一个小的mspaint来告诉你我的意思。让我们举个例子:('This','is','a'),7)。如果('This','is','a')是“i”,那么我需要两个集合或CNT的出现。在这种情况下是7。