1

I want to compare the length of the two dictionaries as well as each key,value pair in each dictionary. I also need to be able to print out if there is no match when looking for it.

My current code seems to pass on the length criteria but fails when trying to match elements:

assert_that(len(model_dict), len(server_dict))
    for x in model_dict:
        if x not in server_dict and model_dict[x] != server_dict[x]:
            print(x, model_dict[x])

server_dict example of one entry in dictionary:

{2847001: [[[-94.8, 28], [-95.4, 28], [-96, 28], [-96.5, 28.1], [-96.667, 28.133], [-97, 28.2], [-97.6, 28.3], [-98.3, 28.4], [-98.9, 28.6], [-99.4, 29], [-99.8, 29.5], [-100, 30], [-100.1, 30.5], [-100.2, 31]]]}

model_dict example of one entry in dictionary:

{2847001: [[-94.8, 28], [-95.4, 28], [-96, 28], [-96.5, 28.1], [-96.667, 28.133], [-97, 28.2], [-97.6, 28.3], [-98.3, 28.4], [-98.9, 28.6], [-99.4, 29], [-99.8, 29.5], [-100, 30], [-100.1, 30.5], [-100.2, 31]]}

user4659009
  • 675
  • 2
  • 5
  • 19

4 Answers4

2

The mistake seems to be in the usage of and in the condition:

x not in server_dict and model_dict[x] != server_dict[x]

If the first condition passes, the second one makes no sense. Try or instead:

x not in server_dict or model_dict[x] != server_dict[x] 
bereal
  • 32,519
  • 6
  • 58
  • 104
1

If you want to check each key and value you can use dict.items and dict.get with a default value:

for k,v  in model_dict.items():
       if server_dict.get(k,object()) != v:
            print(k,v)

If you just wanted any keys that are not the same from either dict you could get the symmetric difference:

unique = model_dict.keys() ^ server_dict # viewkeys() python2
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • that works as intended, but how do I get rid of the extra braces [] in the values of server_dict? I think that's why the damn thing won't match :/ – user4659009 Nov 14 '15 at 19:26
  • @user4659009, you would have to restructure the dict but how did you end up with the extra nesting, did you create each structure or from an outside source? – Padraic Cunningham Nov 14 '15 at 19:27
  • Outside source - json from a rest endpoint. Would doing the following line instead work - braces around the v? `if server_dict.get(k,object()) != [v]:` – user4659009 Nov 14 '15 at 19:29
  • @user4659009, yep but ideally sorting out the root of the problem would be best, wrapping v in a list could potentially cause some bugs – Padraic Cunningham Nov 14 '15 at 19:30
  • Mmm. `for x in context.response.result['features']: for y in x['geometry']['coordinates']: server_response_coordinate_dict.setdefault(x['properties']['event_id'], []).append(y)` Tried a number of things, but can't get it to work as intended. – user4659009 Nov 14 '15 at 19:32
  • 1
    stick the full code in pastebin, pretty sure I know what is happening – Padraic Cunningham Nov 14 '15 at 19:33
0

You need or, not and. If it's is not in server_dict, you don't want to check it there.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

you're overdoing it, that is the error. when you write this

for x in model_dict:
    if x not in server_dict and model_dict[x] != server_dict[x]:
        print(x, model_dict[x])

For each key in model_dict, you're checking whether the same key is not found in server_dict which itself is enough. what you're doing after the and is not at all necessary, and not correct, because you're trying to match the key's value in model_dict to a non-existent key's value in server_dict.

Just do this:

{x: model_dict(x) for x in model_dict if x not in server_dict}
kmario23
  • 57,311
  • 13
  • 161
  • 150
  • I should have been clearer, these two dictionaries are meant to be identical, hence the assumption about finding the key in both of them – user4659009 Nov 14 '15 at 19:01