Python根据键合并嵌套对象

时间:2019-06-08 23:04:39

标签: python python-3.x list dictionary

因此,我有以下嵌套对象,并且我正在尝试根据键specs合并该对象。我该如何解决。

输入:

    [
    {
        "person_name": "bob",
        "metadata": [
            {
                "name": "car",
                "car_details": [
                    {
                        "color": "black",
                        "type": "bmw",
                        "specs": [
                            {
                                "properties": [
                                    {
                                        "info": [
                                            "sedan",
                                            "germany"
                                        ]
                                    },
                                    {
                                        "info": [
                                            "drive",
                                            "expensive"
                                        ]
                                    }
                                ]
                            }
                        ],
                        "description": "amazing car"
                    }
                ]
            },
            {
                "name": "car",
                "car_details": [
                    {
                        "color": "black",
                        "type": "bmw",
                        "specs": [
                            {
                                "properties": [
                                    {
                                        "info": [
                                            "powerful",
                                            "convertable"
                                        ]
                                    },
                                    {
                                        "info": [
                                            "drive",
                                            "expensive"
                                        ]
                                    }
                                ]
                            }
                        ],
                        "description": "amazing car"
                    }
                ]
            }
        ]
    }
]

预期输出:

   [
    {
        "person_name": "bob",
        "metadata": [
            {
                "name": "car",
                "car_details": [
                    {
                        "color": "black",
                        "type": "bmw",
                        "specs": [
                            {
                                "properties": [
                                    {
                                        "info": [
                                            "powerful",
                                            "convertable"
                                        ]
                                    },
                                    {
                                        "info": [
                                            "sedan",
                                            "germany"
                                        ]
                                    },
                                    {
                                        "info": [
                                            "drive",
                                            "expensive"
                                        ]
                                    }
                                ]
                            }
                        ],
                        "description": "amazing car"
                    }
                ]
            }
        ]
    }
]

到目前为止,这是代码:但这不起作用。

from itertools import groupby
import ast, json
headers = ['color', 'type', 'description']

def _key(d):
  # get the key from a dictionary
  return [d.get(i) for i in headers]


def get_specs(b):
  _specs = [c['properties'] for i in b for c in ast.literal_eval(i['specs'])]
  return json.dumps([{'specs': [i for b in _specs for i in b]}])


def merge(d):
  merged_list = [[a, list(b)] for a, b in groupby(sorted(d, key=_key), key=_key)]
  return [{**dict(zip(headers, a)), 'specs': get_specs(b)} for a, b in merged_list]

1 个答案:

答案 0 :(得分:1)

headers = ['color', 'type', 'description']
from pprint import *
from itertools import groupby
def key(x):
    return [x['car_details'][0][i]  for i in headers]
def merge(l):
    b=next(l)
    d=b['car_details'][0]['specs']
    for i in l:
        for j in i['car_details'][0]['specs'][0]['properties']:
            if j not in d[0]['properties']:
                d[0]['properties'].append(j)
    return b
for i in d:    
    t=i['metadata']    
    i['metadata'] =[]
    for k,g in groupby(t,key=key):
        i['metadata'].append(merge(g))

pprint(d)

输出:

 [{'metadata': [{'car_details': [{'color': 'black',
                                 'description': 'amazing car',
                                 'specs': [{'properties': [{'info': ['sedan',
                                                                     'germany']},
                                                           {'info': ['drive',
                                                                     'expensive']},
                                                           {'info': ['powerful',
                                                                     'convertable']}]}],
                                 'type': 'bmw'}],
                'name': 'car'}],
  'person_name': 'bob'}]