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

2024-09-28 22:20:05 发布

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

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

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位置。任何帮助都太棒了!你知道吗


Tags: andonlyfor字典dict词典totalintersect
2条回答

我希望这会有帮助

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中的剩余部分。你知道吗

   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]}

-更新-

def func( lst, intersection):
     if intersection:
         return { k:[ item[k] for item in lst if k in item ]  for k in reduce( lambda x,y:set(x)&set(y), lst ) }
     else:
         return { k:[ item[k] for item in lst if k in item ]  for k in reduce(lambda x,y:set(x).difference(set(y)), lst ) }

>>> func([a,c],False)
{3: [315], 4: [291], 5: [275]}
>>> func([a,b],False)
{4: [291]}
>>> func( [func([a,b],False),func([a,c],False)],True)
{4: [[291], [291]]}

一个问题是:为了最终的结果,你需要把复制品拿出来,或者尝试改进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])}

相关问题 更多 >