I have two dictionaries which order represented by their values.
d1= {570.44: 2, 305.21: 1, 271.94: 0, 463.20: 3, 556.60: 4, 596.27: 5}
d2= {570.44: 537.5, 596.27: 767.0, 556.60: 644.5, 271.94: 285.0, 305.21: 334.5, 463.20: 428.5}
I would like to compare the order, which is defined by the value.
For example, 570.44
won't be the same according to the values, in d1 it would be in the third position, while in d2 it will be in the fourth.
d1[570.44] < d2[570.44]
As you can see, when I mean order I mean the order determined by the value of the keys, not by order of insertion etc.
How can I compare these two dictionaries? (usually the comparisons are for equality like in this question but I am interested in order
CodePudding user response:
If I understand this correctly, this might be what you want. Simply sort the dictionaries by their value and compare the list of ordered keys.
In [10]: d1_ordered = [k for k, _ in sorted(d1.items(), key=lambda x: x[1])]
In [11]: d2_ordered = [k for k, _ in sorted(d2.items(), key=lambda x: x[1])]
In [12]: d1_ordered
Out[12]: [271.94, 305.21, 570.44, 463.2, 556.6, 596.27]
In [13]: d2_ordered
Out[13]: [271.94, 305.21, 463.2, 570.44, 556.6, 596.27]
In [14]: for idx, d1_k in enumerate(d1_ordered):
...: if d1_k not in d2_ordered:
...: print(f"Key {d1_k} not exists in d2")
...: continue
...: if idx != d2_ordered.index(d1_k):
...: print(f"Key {d1_k} is in different order in these dicts")
...:
Key 570.44 is in different order in these dicts
Key 463.2 is in different order in these dicts
CodePudding user response:
Considering that you want to take only the relative order of common keys into account, you can create two lists of keys (sorted by their respective values), and filter out the keys that are not present in the other dictionary.
Then, comparing those two lists for equality should give you your result.
d1= {570.44: 2, 305.21: 1, 271.94: 0, 463.20: 3, 556.60: 4, 596.27: 5}
d2= {570.44: 537.5, 596.27: 767.0, 556.60: 644.5, 271.94: 285.0, 305.21: 334.5, 463.20: 428.5}
k1 = list(filter(lambda k: k in d2, sorted(d1, key=d1.get)))
k2 = list(filter(lambda k: k in d1, sorted(d2, key=d2.get)))
print("Same order") if (k1 == k2) else print("Different order")
CodePudding user response:
Perhaps this will help:-
d1 = {570.44: 2, 305.21: 1, 271.94: 0, 463.20: 3, 556.60: 4, 596.27: 5}
d2 = {570.44: 537.5, 596.27: 767.0, 556.60: 644.5,
271.94: 285.0, 305.21: 334.5, 463.20: 428.5}
d2_keys = list(d2.keys())
for i, k in enumerate(d1.keys()):
try:
if i == d2_keys.index(k):
print(f'Key {k} is in same relative position')
else:
print(f'Key {k} is not in same relative position')
except ValueError:
pass # key in d1 doesn't exist in d2
CodePudding user response:
I want to know if the keys are ordered in the same order, and if not, take some action ; The order is given by the values of the keys
Then, you can simply do: sorted(d1, key=d1.get) == sorted(d2, key=d2.get)
output: False
edit. If the dictionaries have different lengths and you only want to compare the first common items:
d1 = {'a': 1, 'b': 0}
d2 = {'a': 1, 'b': 0, 'c': 2}
k1 = sorted(d1, key=d1.get)
k2 = sorted(d2, key=d2.get)
k1[:len(k2)] == k2[:len(k1)]
output: True