如何编写函数来打印员工的层次结构树 [英] How do I write a function to print out a hierarchy tree of the employees

查看:86
本文介绍了如何编写函数来打印员工的层次结构树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将员工及其老板列表作为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

推荐答案

我可以很好地弄清楚该代码应该做什么。但我认为你应该使用 string.split [ ^ ],而非 string.strip [ ^ ]。
I cannot quite figure out what that code is supposed to do. however I think you should be using string.split[^], not string.strip[^].


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?


这篇关于如何编写函数来打印员工的层次结构树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆