I have a list as follows:
list = ['Item', 'Qty'], ['2', '1'], ['3', '1'], ['4', '1'], ['4.1', '2'], ['4.2', '2'], ['4.3', '2'], ['5', '1'], ['5.1', '1'], ['5.1.1', '1'], ['5.1.2', '1'], ['5.1.2.1', '1'], ['5.1.2.1.1', '1'], ['5.1.3', '1'], ['5.1.3.1', '1'], ['5.2', '1'], ['5.2.1', '1'], ['5.2.2', '1'], ['5.2.2.1', '1'], ['5.2.2.2', '1'], ['5.3', '1'], ['5.4', '1'], ['5.5', '1']
Each item in the list has a quantity field. The list is structured in a hierarchical manner such that each item has a parent or perhaps multiple parents apart from the top level items.
eg. Item 5.1.2.1 has parents 5.1.2, 5.1 and 5 and has one child 5.1.2.1.1. However item 5 only has multiple children.
What I would like to accomplish is to multiply the quantity of an item by the quantities of all its parents in a recursive manner and update the quantities in the list. Or a new list can be generated with the updated values.
I am having some difficulty getting it to work correctly as this is my first attempt at recursion. Any help would be awesome.
https://stackoverflow.com/questions/65720522/python3-recursively-multiply-items-in-hierarchical-list January 14, 2021 at 10:07PM
没有评论:
发表评论