Question: "Regrouping" data from nested dictionaries

Question

"Regrouping" data from nested dictionaries

Answers 1
Added at 2017-11-07 20:11
Tags
Question

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
Answers to

"Regrouping" data from nested dictionaries

nr: #1 dodano: 2017-11-07 21:11

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']
}
Source Show
◀ Wstecz