簡體   English   中英

修改嵌套字典中的鍵和值,其中包含列表

[英]Modify keys and values in a nested dictionary with lists inside of it

如何迭代包含子項的字典,其中包含一個包含更多字典的列表,並在某些條件完成時添加一個值

我有的字典的一部分是這樣的:

{
        "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
                        }]
                }]
 }

我一直試圖在 function 中使用不同的 for 循環來解決它,但我沒有得到想要的結果。

我想做的是為每個訂單1的孩子,go,直到他的最后一個孩子並總結所有價格,然后對訂單2做同樣的事情,依此類推,直到最后一個孩子

我現在已經知道它是一個字典,並且在關鍵子項內部有一個包含更多字典的列表我可以執行幾個 for 循環,如下所示

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

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

但問題是我不知道如何制作一個與下一個孩子反饋的 function,遍歷所有孩子直到結束。

所需的 output 應該是所有級別的鍵,如下所示:

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

我要做的是使用async的遞歸 function ,目的是等到最后一個嵌套級別添加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

我希望它對你有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM