【发布时间】:2018-08-26 18:43:21
【问题描述】:
我正在尝试对数据集进行递归以找到最高级别的项目,即没有父项的项目。
结构如下:
╔════════════╦════════════╗
║ Item ║ Material ║
╠════════════╬════════════╣
║ 2094-00003 ║ MHY00007 ║
║ 2105-0001 ║ 2105-0002 ║
║ 2105-0002 ║ 2105-1000 ║
║ 2105-1000 ║ 2105-1003 ║
║ 2105-1003 ║ 7547-122 ║
║ 7932-00001 ║ 7932-00015 ║
║ 7932-00002 ║ 7932-00015 ║
║ 7932-00010 ║ MHY00007 ║
║ 7932-00015 ║ 7932-05000 ║
║ 7932-05000 ║ MHY00007 ║
╚════════════╩════════════╝
因此,例如,如果我选择 7547-122,该函数将返回 2105-0001。所以函数递归地沿着树向上,7547-122 -> 2105-1003 -> 2105-1000 -> ... -> 2105-0001。
当我运行我的代码时,我只能让它返回一个顶层,从 MHY00007 案例中可以看出,有时有多个顶层。如何返回任何给定材料具有的所有顶级列表?
我的代码:
import pandas as pd
class BillOfMaterials:
def __init__(self, bom_excel_path):
self.df = pd.read_excel(bom_excel_path)
self.df = self.df[['Item', 'Material']]
def find_parents(self, part_number):
material_parent_search = self.df[self.df.Material == part_number]
parents = list(set(material_parent_search['Item']))
return parents
def find_top_levels(self, parents):
top_levels = self.__ancestor_finder_([parents])
print(f'{parents} top level is {top_levels}')
return {parents: top_levels}
def __ancestor_finder_(self, list_of_items):
for ancestor in list_of_items:
print(f'Searching for ancestors of {ancestor}')
ancestors = self.find_parents(ancestor)
print(f'{ancestor} has ancestor(s) {ancestors}')
if not ancestors:
return ancestor
else:
highest_level = self.__ancestor_finder_(ancestors)
return highest_level
BOM = BillOfMaterials(bom_excel_path="Path/To/Excel/File/BOM.xlsx")
ItemsToSearch = ['7547-122', 'MHY00007']
top_levels = []
for item in ItemsToSearch:
top_levels.append(BOM.find_top_levels(item))
【问题讨论】:
标签: python python-3.x pandas recursion tree