我查了字典的交叉点,并试图使用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位置。任何帮助都太棒了!你知道吗
我希望这会有帮助
类似地,你可以用集合的“差”来找出a,b和c中的剩余部分。你知道吗
输出:
对于其他问题,只需减少lst元素
还可以将其转换为函数
示例:
-更新-
一个问题是:为了最终的结果,你需要把复制品拿出来,或者尝试改进func本身。你知道吗
相关问题 更多 >
编程相关推荐