我查找了词典的交叉点,并尝试使用设置库,但无法弄清楚如何显示值而不只是拉出键来与它们一起工作,所以我希望寻求帮助。我有三个随机长度的词典:
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,但重要的是所有这些值都保留在各自的位置,这意味着我无法在C位置拥有A值。任何帮助都会很棒!
答案 0 :(得分:1)
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])}
答案 1 :(得分:0)
我希望这可能会有所帮助
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中找到剩余物。