0

I'm currently parsing through JSON as a quick example this is what it looks like:

Dictionary 1:

data:
    0:
        tag "importanttagone"
        value   233
    1:  
        tag "importanttagtwo"
        value   234

Dictionary 2:

data:
    0:
        name    "Important Tag One"
        tag     "importanttagone"
    1:
        name    "Important Tag Two"
        tag     "importanttagtwo"

So essentially what I want is to display the name and the value associated.

Expected Output:

Name: Important Tag On

Value: 233  

Is there a way to compare both tags in the dictionaries and then get the name that's within the same key as the tag?

4 Answers4

1
d1 = {
    "data":{
        0: {
            "tag": "importanttagone",
            "value": 233
        },
        1: {
            "tag": "importanttagtwo",
            "value": 234
        }
    }
}

d2 = {
    "data":{
        0: {
            "tag": "importanttagone",
            "name": "Important Tag One"
        },
        1: {
            "tag": "importanttagtwo",
            "name": "Important Tag Two"
        }
    }
}

d1_flat = {v['tag']: v['value'] for _, v in d1['data'].items()}
d2_flat = {v['tag']: v['name'] for _, v in d2['data'].items()}

result = {}

for k, v in d1_flat.items():
    name = d2_flat.get(k, None)
    if name:
        result[name] = v

print(result)

Output:

{'Important Tag One': 233, 'Important Tag Two': 234}
Alexey
  • 409
  • 2
  • 8
0

If the same tags has the same keys in the dicts:

[(d1[key]['name'], d2[key]['value']) for key in d1 if key in d2]
0

compare the two dictionaries maybe element-wise by iteration through both dicts:

for k1 in dict1:
   for k2 in dict2:
      if dict1[k1]['tag'] == dict2[k2]['tag']
         print('Name  : ' + dict[k1]['name'])
         print('Value : ' + dict[k2]['value'])
Dorian
  • 1,439
  • 1
  • 11
  • 26
  • Thanks, but I've tried this before and I always get: ' KeyError: 'tag' ' – Jason Stratter Nov 19 '17 at 21:30
  • I added ['data'] to the first two lines. for k1 in dict1['data']: for k2 in dict2['data']: but now i get: TypeError: unhashable type: 'dict' – Jason Stratter Nov 19 '17 at 21:45
  • 1
    I'm not 100% sure about your dictionary structure. is it like the one explained by @Alexey ? You have 2 dicts, dict1 and dict2 with the same tags? ever considered putting it in one and the same dict, since you are just doubling your necessary memory? – Dorian Nov 19 '17 at 21:50
  • I'm not exactly sure. Firefox listed it like that, however, Chrome doesn't have those 0,1,2...etc. Is it possible to combine dictionaries that are parsed from the internet? I didn't even think about that. The problem with that is one dictionary has more keys than the other – Jason Stratter Nov 19 '17 at 21:54
  • have a look at that: https://stackoverflow.com/a/5946359/7813847 same approach as for efficient SQL databases – Dorian Nov 19 '17 at 21:59
0

You can try One line solution :

#data from @Alexey

d1 = {
    "data":{
        0: {
            "tag": "importanttagone",
            "value": 233
        },
        1: {
            "tag": "importanttagtwo",
            "value": 234
        }
    }
}

d2 = {
    "data":{
        0: {
            "tag": "importanttagone",
            "name": "Important Tag One"
        },
        1: {
            "tag": "importanttagtwo",
            "name": "Important Tag Two"
        }
    }
}

print({item.get('value'):sub_item.get('name') for item in d1['data'].values()for sub_item in d2['data'].values()if item.get('tag') in sub_item.values()})

output:

{233: 'Important Tag One', 234: 'Important Tag Two'}
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88