Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将目录列表转换为嵌套目录列表_Python_Json_Dictionary - Fatal编程技术网

Python 将目录列表转换为嵌套目录列表

Python 将目录列表转换为嵌套目录列表,python,json,dictionary,Python,Json,Dictionary,我有一个这样的基础数据,数据库中唯一的id是d_id。(数据名为“输入”) 我想制作如下字典,这是一个按位置id、rest id和d id分类的嵌套json: 地点将有多个位置,每个位置将有多个rest_id,每个rest将有多个d_id { "localities" : [ { "location": "ABC", "location_id": 1, "rest_details": [

我有一个这样的基础数据,数据库中唯一的id是d_id。(数据名为“输入”)

我想制作如下字典,这是一个按位置id、rest id和d id分类的嵌套json:

地点将有多个位置,每个位置将有多个rest_id,每个rest将有多个d_id

{ 
    "localities" : [
        { 
            "location": "ABC",
            "location_id": 1,
            "rest_details": [
                {
                    "rest_id": 2, 
                    "rest_name": "Grilling", 
                    "d_details" : [
                        {
                            "d_id" : "a1",
                            "d_name" : "Fishing"
                        },
                        {
                            "d_id" : "a3",
                            "d_name" : "Fishing2"
                        }
                    ]
                },
                {
                    "rest_id": 3,
                    "rest_name": "Kayaking",
                    "d_details" : [
                        {
                            "d_id" : "a2",
                            "d_name" : "catching"
                        }
                    ]
                }
            ]
        },
        {
            "location" : "DEF",
            "location_id": 2,
            "rest_details": [
                {
                    "rest_id" : 4,
                    "rest_name" : "Careoff",
                    "d_details" : [
                        {
                            "d_id" : "a4",
                            "d_name": "Watering"
                        }
                    ]
                }
            ]
        }
    ]
}
在过去的几天里,我尝试了以下操作,其中我尝试将数据分离为位置信息、剩余信息和数据信息:

位置信息 对于唯一位置信息条目 关于d_信息 用于将剩余信息与位置信息合并
tb不会产生预期结果的第一段。

您的代码中有错误。您至少应该自己运行每个代码段,看看它们是否提供了所需的步骤输出(因为它们没有)

除此之外,你的方法似乎不必要地复杂。我会这样尝试:

locations = {}
for elem in input:
    rest_details = []
    curr_rest_detail = {"rest_id": elem["rest_id"], "rest_name":elem["rest_name"], "d_details":[{"d_id":elem["d_id"], "d_name":elem["d_name"]}]}
    try:
        rest_details = locations[elem["location_id"]]["rest_details"]
        rest_detail_exists = False
        for detail in rest_details:
            if detail["rest_id"] == curr_rest_detail["rest_id"]:
                rest_detail_exists = True
                detail["d_details"].append({"d_id":elem["d_id"], "d_name":elem["d_name"]})
                break
        if not rest_detail_exists:
            rest_details.append(curr_rest_detail)
    except KeyError:
        locations[elem["location_id"]] = {"location": elem["location"], "location_id":elem["location_id"], "rest_details":[curr_rest_detail]}

result = {"localities": [value for value in locations.values()]}

这段代码基本上迭代输入的元素,并在条目不存在时添加条目。如果它已经存在,它只会附加附加信息。

您应该先尝试您想要的,然后寻求帮助。如果你曾经尝试过,那么请分享。你基本上是在要求人们为你做这件事。这个网站是关于帮助有特定问题的人,而不是免费的编码服务。@TanveerAlam的目的是在这个问题上寻求帮助,我作为一个新用户得到了纠正。道歉。我已经添加了我尝试的信息。“如果你能给我一个指导,我会帮上大忙的。”保罗·鲁尼的本意是在这件事上寻求帮助,而我作为一个新用户被纠正了。道歉。我已经添加了我尝试的信息。如果你能提供指导,那会有很大帮助。@TanveerAlam:看来我在这里遗漏了一些与列表理解相关的非常基本的东西。@x平方-真的谢谢你。这比我想做的要好得多。将了解有关尝试和例外的更多信息。
location_info = []
location_fields = {'location', 'location_id', 'd_id', 'rest_id' }
for item in input:
    loc = {key:value for key,value in item.items() if key in  location_fields}
location_info.append(loc)
k=1
while k<len(location_info):
    if location_info[k] == location_info[k-1]:
        location_info.pop(k-1)
    else:
        k = k+1
rest_info = []
rest_fields = {'rest_name','rest_id', 'd_id', 'loc_id'}
for item in input:
    res = {key:value for key,value in item.items() if key in restaurant_fields}
    restaurant_info.append(res)
d_info = []
d_fields = {'d_id', 'd_name', 'location_id'}
for item in input:
    dis = {key:value for key,value in item.items() if key in d_fields}
d_info.append(dis)
for ta in location_info:
    for tb in rest_info:
        if ta['loc_id'] == tb['loc_id']:
            ta['localities'] = tb
locations = {}
for elem in input:
    rest_details = []
    curr_rest_detail = {"rest_id": elem["rest_id"], "rest_name":elem["rest_name"], "d_details":[{"d_id":elem["d_id"], "d_name":elem["d_name"]}]}
    try:
        rest_details = locations[elem["location_id"]]["rest_details"]
        rest_detail_exists = False
        for detail in rest_details:
            if detail["rest_id"] == curr_rest_detail["rest_id"]:
                rest_detail_exists = True
                detail["d_details"].append({"d_id":elem["d_id"], "d_name":elem["d_name"]})
                break
        if not rest_detail_exists:
            rest_details.append(curr_rest_detail)
    except KeyError:
        locations[elem["location_id"]] = {"location": elem["location"], "location_id":elem["location_id"], "rest_details":[curr_rest_detail]}

result = {"localities": [value for value in locations.values()]}