使用其中的列表修改嵌套字典中的键和值
Modify keys and values in a nested dictionary with lists inside of it
如何在包含 children 的字典中迭代,其中包含一个包含更多字典的列表,并在满足某些条件时添加一个值
我的部分字典是这样的:
{
"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的children,走到他的最后一个child,把所有的价格相加,然后对订单2做同样的事情,然后以此类推,直到最后 child
我现在已经知道它是一本字典了,在键里面 children 有一个包含更多字典的列表我可以像下面这样做几个 for 循环
list_segments = []
first_children = data['children']
for dicts in first_children:
for segment in dicts['children']:
list_segments.append(segment)
但问题是我不知道如何制作一个反馈下一个 child 的函数,遍历所有 children 直到结束。
所需的输出应该是所有级别的关键,如下所示:
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
的递归函数,目的是等到最后一个嵌套级别添加 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
。
希望对你有所帮助
如何在包含 children 的字典中迭代,其中包含一个包含更多字典的列表,并在满足某些条件时添加一个值
我的部分字典是这样的:
{
"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的children,走到他的最后一个child,把所有的价格相加,然后对订单2做同样的事情,然后以此类推,直到最后 child
我现在已经知道它是一本字典了,在键里面 children 有一个包含更多字典的列表我可以像下面这样做几个 for 循环
list_segments = []
first_children = data['children']
for dicts in first_children:
for segment in dicts['children']:
list_segments.append(segment)
但问题是我不知道如何制作一个反馈下一个 child 的函数,遍历所有 children 直到结束。
所需的输出应该是所有级别的关键,如下所示:
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
的递归函数,目的是等到最后一个嵌套级别添加 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
。
希望对你有所帮助