Ttj
Ttj

Reputation: 13

"Regrouping" data from nested dictionaries

I have a JSON document like this:

{
'item1':{
   'value':[a,b],
   'number':1
  },
'item2':{
    'value':[a],
    'number':2
  },
 'item3':{
    'value':[b],
    'number':3
  }
}

Is it possible to get item names grouped by values?

Here's what I want:

a: item1, item2
b: item1, item3

I tried the following, but it didn't work:

l = sorted(l.iteritems())
for key, group in groupby(l,key=lambda x:x['value']):
   print key, group

Upvotes: 1

Views: 316

Answers (1)

schesis
schesis

Reputation: 59148

Firstly, that's not JSON.

Strings in JSON have double, not single quotes, and the unquoted tokens a and b are meaningless in JSON. Assuming that your actual document looks something like this:

{
  "item1": {
    "value": ["a", "b"],
    "number": 1
  },
  "item2": {
    "value": ["a"],
    "number": 2
  },
  "item3": {
    "value": ["b"],
    "number": 3
  }
}

… and you've loaded it into a Python dictionary data, you can get the output you want as follows:

grouped = {}
for key, subdict in data.items():
    for value in subdict['value']:
        grouped.setdefault(value, []).append(key)

Which will leave you with grouped looking like this:

{
    'a': ['item2', 'item1'],
    'b': ['item3', 'item1']
}

Upvotes: 1

Related Questions