修改嵌套字典中的键和值,其中的列表在i中

2024-04-25 05:50:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在包含子级的dict中进行迭代,该dict包含一个包含更多字典的列表,并且在完成某个条件时添加一个值

我的字典有一部分是这样的:

{
        "a": "segments",
        "children":
            [{"a": 1,
                "order": "1",
                "price": 1500.0,
                "children":
                    [{
                        "a": "1.1",
                        "order": "2",
                        "price": 75.0,
                        "children": 
                            [{
                                "a": "1.1.1",
                                "order": "3",
                                "price":100.0
                            }]
                    }]

                            // . . . 
                },
                {"a": n,
                "order": "1",
                "price": 100.0,
                "children": 
                        [{
                        "a": "n.1",
                        "order": "2",
                        "price": 1000.0
                        }]
                }]
 }

我一直在尝试在函数中使用不同的for循环来解决这个问题, 但我没有达到预期的效果。你知道吗

我要做的是,对每一个有订单1的孩子,一直到他最后一个孩子,把所有的价格加起来,然后对订单2做同样的事情,依此类推,直到最后一个孩子

我已经知道了它是一个怎样的字典,在关键的孩子们里面有一个包含更多字典的列表,我可以为下面的循环做几个

list_segments = []
first_children = data['children']
for dicts in first_children:

    for segment in dicts['children']:
        list_segments.append(segment)

但问题是,我不知道如何制作一个函数,与下一个孩子一起反馈,遍历所有的孩子直到最后。你知道吗

期望的输出应该是所有级别中的一个键,如下所示:

level_price: price order 1 +price order 2 + price price order 3
    level_price: price order 2 + price price order 3
        level_price: price order 3

Tags: 函数订单列表for字典孩子orderlevel
1条回答
网友
1楼 · 发布于 2024-04-25 05:50:20

我要做的是使用async的递归函数,目的是等待最后一个嵌套级别添加price

async def mainfunction(current_level):

    if 'subtree_price' in current_level:
        for child in current_level['children']:
            if 'children' in child:            
                    child = await  mainfunction(child)
    else:     
        current_level = await parentFunction(current_level)       
        if 'children' in current_level:            
            for child in current_level['children']:            
                child = await  mainfunction(child)       
    return current_level

async def parentFunction(current_level):    
    counter = 0    
    if 'price' in current_level:
        if 'children' in current_level:       
            for child in current_level['children']:
                counter += await childfunction(0, child)        
        else:
            current_level['subtree_price'] = current_level['price']           
        current_level['subtree_price'] = counter +  current_level['price']               
    return current_level

async def childfunction(counter, current_level):   
    counter = 0 
    if 'children' in current_level:  
        for child in current_level['children']:
            counter += child['price']
            if 'children' in child:   
                counter += await childfunction(counter, child)        
    return counter + current_level['price']

首先你要做的是把从当前级别到最后一个级别的所有prices加起来。它可以通过childfunction完成

mainfunction检查您当前的级别(order)是否已经添加了childfunction所做的总和(检查subtree_price是否存在)。如果不存在,则调用parentfunction,检查是否存在子级。如果它们存在,它将执行childfunction,结果将添加到当前级别的parent。否则,subtree_price将是您自己的price。你知道吗

希望对你有帮助。你知道吗

相关问题 更多 >