问题描述
将员工及其老板列表作为csv文件,编写一个打印出员工层级树的函数。
样本输入= Sam ,Ian,技术负责人,2009 / Ian,NULL,CEO,2007 / Fred,Sam,开发人员,2010
格式是姓名,主管,名称,年份加入
输出应该是
Ian CEO 2007
-Sam技术主管2009
- 免费开发者2010
我尝试过:
Given a list of employees and their bosses as a csv file , write a function that will print out a hierarchy tree of the employees.
Sample input = Sam, Ian, technical lead, 2009 / Ian, NULL, CEO,2007/ Fred, Sam, developer, 2010
The format is name, supervisor, designation, year of joining
The output should be
Ian CEO 2007
-Sam Technical lead 2009
--Fred Developer 2010
What I have tried:
import csv
with open("list.csv", 'r') as csv_file:
list_reader = csv.reader(csv_file, delimiter=",")
employee_list = {
name: boss.strip() for name, boss, designation, year in list_reader}
mgrs = [k for k, v in employee_list.items() if v == '']
while mgrs:
print ", ".join(mgrs)
mgrs = [k for k, v in employee_list.items() if v in mgrs]
name:boss.strip()for name,boss, list_reader中的名称,年份
ValueError:要解压的值太多
name: boss.strip() for name, boss, designation, year in list_reader}
ValueError: too many values to unpack
推荐答案
strq = "Sam, Ian, technical lead, 2009 / Ian, NULL, CEO,2007/Fred, Sam, developer, 2010"
def treeEmployee(infoStr):
str1 = infoStr.split("/")
s2 = []
for i in str1:
s2.append(i.split(","))
for i in range(len(s2)):
for j in range(1, len(s2)):
if s2[i][1] == s2[j][0]:
s2[i], s2[j] = s2[j], s2[i]
return s2
可以我们这样做?
Can we do it this way?
这篇关于如何编写函数来打印员工的层次结构树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!