Python 给定3个大小不同的DICT,我如何找到交点和值?

Python 给定3个大小不同的DICT,我如何找到交点和值?,python,dictionary,Python,Dictionary,我查找了字典的交叉点,并尝试使用集合库,但不知道如何显示值,而不仅仅是拉出键来使用它们,所以我希望得到一些帮助。我有三本随机长度的字典: dict_a= {1: 488, 2: 336, 3: 315, 4: 291, 5: 275} dict_b={2: 0, 3: 33, 1: 61, 5: 90, 15: 58} dict_c= {1: 1.15, 9: 0, 2: 0.11, 15: 0.86, 19: 0.008, 20: 1834} 我需要找出字典A、B和C中的键,并将它们合并

我查找了字典的交叉点,并尝试使用集合库,但不知道如何显示值,而不仅仅是拉出键来使用它们,所以我希望得到一些帮助。我有三本随机长度的字典:

dict_a= {1: 488, 2: 336, 3: 315, 4: 291, 5: 275}

dict_b={2: 0, 3: 33, 1: 61, 5: 90, 15: 58}

dict_c= {1: 1.15, 9: 0, 2: 0.11, 15: 0.86, 19: 0.008, 20: 1834}
我需要找出字典A、B和C中的键,并将它们合并到一个新字典中。然后,我需要找出字典A&B或A&C或B&C中的键,并将它们放到新字典中。我应该在A、B和C中留下的是该词典所特有的

因此,最终,我会使用单独的词典,如下所示:

total_intersect= {1: {488, 61, 1.15}, 2: {336, 0, 0.11}}
A&B_only_intersect = {3: {315,33}, 5:{275,90}} (then dicts for A&C intersect and B&C intersect)
dict_a_leftover= {4:291} (and dicts for leftovers from B and C)
我考虑过使用zip,但重要的是所有这些值都保留在各自的位置,这意味着我不能将A值放在C位置。任何帮助都会很棒

   lst =  [dict_a,dict_b,dict_c] 
   total_intersect_key = set(dict_a) & set(dict_b) & set(dict_c)
   total_intersect = { k:[ item[k] for item in lst ]  for k in total_intersect_key}
输出:

{1: [488, 61, 1.15], 2: [336, 0, 0.11]}
对于其他问题,只需减少lst元素

lst = [dict_a,dict_b]
A&B_only_intersect = { k:[ item[k] for item in lst ]  for k in set(dict_a.keys) & set(dict_b)}
还可以将其转换为函数

def intersect(lst):
     return { k:[ item[k] for item in lst if k in item ]  for k in reduce( lambda x,y:set(x)&set(y), lst ) }
例如:

>>> a
{1: 488, 2: 336, 3: 315, 4: 291, 5: 275}
>>> b
{1: 61, 2: 0, 3: 33, 5: 90, 15: 58}
>>> c
{1: 1.15, 2: 0.11, 9: 0, 15: 0.86, 19: 0.008, 20: 1834}
>>> intersect( [a,b] )
{1: [488, 61], 2: [336, 0], 3: [315, 33], 5: [275, 90]}
>>> intersect( [a,c] )
{1: [488, 1.15], 2: [336, 0.11]}
>>> intersect( [b,c] )
{1: [61, 1.15], 2: [0, 0.11], 15: [58, 0.86]}
>>> intersect( [a,b,c] )
{1: [488, 61, 1.15], 2: [336, 0, 0.11]}
-----更新-----

有一个问题:你需要将复制品取出以获得最终结果,或者尝试改进func本身

{k:set( reduce( lambda x,y:x+y, v) ) for k,v in func( [func([a,b],False),func([a,c],False)],True).iteritems()}

{4: set([291])}
我希望这能有所帮助

dict_a= {1: 488, 2: 336, 3: 315, 4: 291, 5: 275}
a = set(dict_a)
dict_b={2: 0, 3: 33, 1: 61, 5: 90, 15: 58}
b =  set( dict_b)
dict_c= {1: 1.15, 9: 0, 2: 0.11, 15: 0.86, 19: 0.008, 20: 1834}
c = set( dict_c )

a_intersect_b = a & b 

a_intersect_c = a & c

b_intersect_c = b & c

a_interset_b_intersect_c = a_intersect_b & c


total_intersect = {}  
for id in a_interset_b_intersect_c:
    total_intersect[id] = { dict_a[id] , dict_b[id] , dict_c[id] }

print total_intersect


a_b_only_intersect = {}
for id in a_intersect_b:
    a_b_only_intersect[id] =  { dict_a[id] , dict_b[id] }

print a_b_only_intersect

b_c_only_intersect = {}
for id in b_intersect_c:
    b_c_only_intersect[id] =  { dict_b[id] , dict_c[id] }

print b_c_only_intersect

a_c_only_intersect = {}
for id in a_intersect_c:
    a_c_only_intersect[id] =  { dict_a[id] , dict_c[id] }

print a_c_only_intersect

类似地,你可以使用集合的“差异”在a、b和c中找到剩余的键。

要查看所有键中出现的键:
common\u keys=dict\u a.keys()&dict\u b.keys()&dict\u c.keys()
(如果使用python2将
.keys()
替换为
.viewkeys()
)。然后试着想出一种构建组合字典的方法。哪种版本的Python?视图对象可能会执行您需要的操作,但语法在2和3之间有所不同。“我不能在C位置有一个值”您使用的是dict和set,两者都是无序的。你说的这个“位置”是什么?我想他希望列表作为结果dicts中的值,而不是他错误地编写的集合。对不起@Rawing,基本上我的意思是,如果我将所有三个字典相加,我会得到1:有3个值,但3:只有两个值。最终,这些都将进入Excel电子表格,因此A需要全部对齐,B需要对齐,等等。哇,函数示例非常优雅,谢谢@用户3097421不是问题。那么,如果我想找到只在a上的人,而不是在B或C上的人,我应该先找到所有的交点,然后做一些集合数学吗?我真的不理解更新。我现在是不是说,对于a,b相交,我想要的是不相交的?我需要把它分开,所以我需要剩下的a,剩下的b和剩下的c。@user3097421所以你想要的是a&~(b|c)?。新的更新是首先查找a和b之间的差异,然后查找a和c之间的差异。最后,查找第一部分结果之间的交点。新参数使您能够进行差分或相交。
dict_a= {1: 488, 2: 336, 3: 315, 4: 291, 5: 275}
a = set(dict_a)
dict_b={2: 0, 3: 33, 1: 61, 5: 90, 15: 58}
b =  set( dict_b)
dict_c= {1: 1.15, 9: 0, 2: 0.11, 15: 0.86, 19: 0.008, 20: 1834}
c = set( dict_c )

a_intersect_b = a & b 

a_intersect_c = a & c

b_intersect_c = b & c

a_interset_b_intersect_c = a_intersect_b & c


total_intersect = {}  
for id in a_interset_b_intersect_c:
    total_intersect[id] = { dict_a[id] , dict_b[id] , dict_c[id] }

print total_intersect


a_b_only_intersect = {}
for id in a_intersect_b:
    a_b_only_intersect[id] =  { dict_a[id] , dict_b[id] }

print a_b_only_intersect

b_c_only_intersect = {}
for id in b_intersect_c:
    b_c_only_intersect[id] =  { dict_b[id] , dict_c[id] }

print b_c_only_intersect

a_c_only_intersect = {}
for id in a_intersect_c:
    a_c_only_intersect[id] =  { dict_a[id] , dict_c[id] }

print a_c_only_intersect