2

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

Darren Christopher
  • 3,893
  • 4
  • 20
  • 37
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 2
    I'm not sure if I understand the question. Do you mean that you want to know if a key exists in both dictionaries but in a different relative position? –  Sep 14 '21 at 07:36
  • 1
    Are you looking for `OrderedDict`? – ravi Sep 14 '21 at 07:36
  • @DarkKnight yes, I want to know if the keys are ordered in the same order, and if not, take some action – KansaiRobot Sep 14 '21 at 07:41
  • What do you consider equal? Both dictionaries have the same keys in the same order? Can one dictionary have keys that are not in the other dictionary and will they still be considered equal if the common keys are in the same order? – Robby Cornelissen Sep 14 '21 at 07:42
  • @RobbyCornelissen I am not evaluating equality (since the values of the keys are different). I am evaluating order. – KansaiRobot Sep 14 '21 at 07:43
  • @RobbyCornelissen Also, yes d2 can have less keys than d1, the thing is that the remaining are in the same order – KansaiRobot Sep 14 '21 at 07:44
  • @RaviJoshi I checked `OrderedDict` and it seems that is not what I want, since that only *preserves* the order of insertion. – KansaiRobot Sep 14 '21 at 07:49
  • @KansaiRobot can you check my update? – mozway Sep 14 '21 at 07:57

4 Answers4

1

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")
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
1

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
Darren Christopher
  • 3,893
  • 4
  • 20
  • 37
0

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

mozway
  • 194,879
  • 13
  • 39
  • 75
0

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