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. Using my example that's the variable I would like to have:
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.
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:
>>> 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.
For example, if Dict1
and Dict2
were of the following form:
>>> 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',)))]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.