简体   繁体   English

比较元组和列表的字典

[英]Comparing dictionaries of tuples and lists

I have found multiple posts (this one too) that was about this topic but none of those options would work for my dictionaries.我找到了多篇关于这个主题的帖子(这个也是),但这些选项都不适用于我的字典。 I wanna compare my dictionnaries and know the number of values identical and also the pair of key-values unique to each dictionaries.我想比较我的字典并知道相同的值的数量以及每个字典唯一的键值对。

I am working with two dictionaries with Tuples as key and list as value (where the second value is another list) as follow:我正在使用两个字典,其中元组作为键,列表作为值(第二个值是另一个列表),如下所示:

Dict1:{(10, 11): ['C', ['T']],
       (20, 21): ['C', ['T']],
       (34, 35): ['G', ['A']],
       (68, 69): ['A', ['T','G']]}


Dict2:{(10, 11): ['C', ['T']],
       (20, 21): ['C', ['A']],
       (40, 41): ['T', ['G']],
       (68, 69): ['A', ['T','G']]}

and I would like to compare those dictionnary and have different output.我想比较那些字典并有不同的 output。 Using my example that's the variable I would like to have:使用我的示例,这就是我想要的变量:

  • 2 values are identical and present in both dict 2个值相同并且存在于两个字典中
  • 2 values are only in dict1 2 个值仅在 dict1 中
  • 2 values are only in dict2 2 个值仅在 dict2 中

I was about to loop over dict1 and compare each key to all dict2 each time (and having variables that i'll updates each time a condition is met) but I am aware that it is probably not the most efficient way of doing it.我正要遍历 dict1 并每次将每个键与所有 dict2 进行比较(并且每次满足条件时我都会更新变量),但我知道这可能不是最有效的方法。

Does anyone have a quicker idea?有人有更快的想法吗?

Thanks谢谢

You can first filter out the keys using set-like methods of dict.keys() objects , and then proceed to get the pairs:您可以首先使用set-like methods of dict.keys() objects过滤掉键,然后继续获取对:

>>> same_keys = Dict1.keys() & Dict2.keys()
>>> dict1_unq_keys = Dict1.keys() - Dict2.keys()
>>> dict2_unq_keys = Dict2.keys() - Dict1.keys()
>>> same_pairs = [(key, Dict1[key]) for key in same_keys if Dict1[key] == Dict2[key]]
>>> Dict1_unq_pair = [(key, Dict1[key]) for key in dict1_unq_keys]
>>> Dict2_unq_pair = [(key, Dict2[key]) for key in dict2_unq_keys]

>>> same_pairs
[((68, 69), ['A', ['T', 'G']]), ((10, 11), ['C', ['T']])]

>>> Dict1_unq_pair
[((34, 35), ['G', ['A']])]

>>> Dict2_unq_pair
[((40, 41), ['T', ['G']])]

Note:笔记:

If it is possible for you to use tuples as dict values instead of lists , this could be done more easily, by directly using dict.items() set operations to get the same pairs.如果您可以将tuples用作dict值而不是lists ,则可以通过直接使用dict.items()设置操作来获得相同的对来更轻松地完成此操作。

For example, if Dict1 and Dict2 were of the following form:例如,如果Dict1Dict2具有以下形式:

>>> Dict1
{(10, 11): ('C', ('T',)),
 (20, 21): ('C', ('T',)),
 (34, 35): ('G', ('A',)),
 (68, 69): ('A', ('T', 'G'))}

>>> Dict2
{(10, 11): ('C', ('T',)),
 (20, 21): ('C', ('A',)),
 (40, 41): ('T', ('G',)),
 (68, 69): ('A', ('T', 'G'))}

# Then you could simple do:
>>> same_pairs = list(Dict1.items() & Dict2.items())

>>> same_pairs
[((68, 69), ('A', ('T', 'G'))), ((10, 11), ('C', ('T',)))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM