df
Employee Id Manager ID
1 3
2 1
3 4
4 NULL
5 NULL
6 7
7 5 and so on
因此,4和5个emp id是CXO。继承人的预期产出:(经理对下属员工)
1 -> 2
2 -> None
3 -> 1,2
4 -> 3,1,2
5 -> 7,6
6 -> None
7 -> 6
例如,4是3(1级)的经理,3是1(2级)的经理,1是2(3级)的经理
为创建员工经理字典而编写的代码:(根据以下输入进行了一些更改)
#convert pandas to list for employee to manager mapping
list22=df.set_index('Employee Id').T.to_dict('list')
查找员工继承人的代码:(非常有效)
# Recursive DP function to find all employees who directly or indirectly
# reports to a given manager and store the result in the result dict
def findAllReportingEmployees(manager, managerToEmployeeMappings, result):
# if the sub-problem is already seen before
if manager in result:
# return the already computed mapping
return result.get(manager)
# find all employees reporting directly to the current manager
managerEmployees = managerToEmployeeMappings.get(manager)
if managerEmployees:
# find all employees reporting in-directly to the current manager
for reportee in managerEmployees.copy():
# find all employees reporting to the current employee
employees = findAllReportingEmployees(reportee, managerToEmployeeMappings,
result)
# move those employees to the current manager
if employees:
managerEmployees.extend(employees)
# save the result to avoid re-computation and return it
result[manager] = managerEmployees
return managerEmployees
# Find all employees who directly or indirectly reports to a manager
def findEmployees(employeeToManagerMappings):
# store manager to employee mappings in a dict
# is used since a manager can have several employees mapped
managerToEmployeeMappings = {}
# fill above dict with the manager to employee mappings
for employee, manager in employeeToManagerMappings.items():
# don't map an employee with itself
if employee != manager:
managerToEmployeeMappings.setdefault(manager, []).append(employee)
# construct an ordered dict to store the result
result = {}
# find all reporting employees (direct and indirect) for every manager
# and store the result in a dict
for key in employeeToManagerMappings.keys():
findAllReportingEmployees(key, managerToEmployeeMappings, result)
# print contents of the result dict
for key, value in result.items():
print(key, "->", value)
直到上述功能完美运行
问题代码:
if __name__ == '__main__':
# construct a dictionary of employee to manager mappings
employeeToManagerMappings = list22 #error here
findEmployees(employeeToManagerMappings)
>**Error:**
>TypeError Traceback (most recent call last)
<ipython-input-27-b35721965687> in <module>
57 # construct a dictionary of employee to manager mappings
58 employeeToManagerMappings = list22
---> 59 findEmployees(employeeToManagerMappings)
<ipython-input-27-b35721965687> in findEmployees(employeeToManagerMappings)
38 # don't map an employee with itself
39 if employee != manager:
---> 40 managerToEmployeeMappings.setdefault(manager, []).append(employee)
41
42 # construct an ordered dict to store the result
TypeError: unhashable type: 'list'
如果我在代码中使用虚拟数据,并将最后4行替换为以下内容,那么它将非常有效
if __name__ == '__main__':
# construct a dictionary of employee to manager mappings
employeeToManagerMappings = {'A': 'A', 'B': 'A', 'C': 'B',
'D': 'B', 'E': 'D', 'F': 'E'}
findEmployees(employeeToManagerMappings)
主要问题发生在转换为list后,我试图在pandas dataframe上运行函数时
问题是您正在指定内置类型
到你的变量
自己检查一下
您需要为变量分配一些列表,最好不要隐藏内置类型,例如list,然后将其作为参数调用函数
相关问题 更多 >
编程相关推荐