Python如何筛选字典中符合条件的键值对 实现员工绩效淘汰需求
代码问题梳理
- 没有按题目要求封装成独立的员工识别函数
- 仅取了绩效排序第一的员工,若存在多名绩效<50的员工会漏处理
- 剩余员工未按字母顺序排序输出,不符合题目要求
修正后符合要求的代码
staff = { 'Joe': { 'position': 'IT', 'performance': 71 }, 'Ana': { 'position': 'IT', 'performance': 65 }, 'Johnson': { 'position': 'IT', 'performance': 49 }, 'Jane': { 'position': 'IT', 'performance': 53 } } # 按要求编写识别并移除绩效不佳员工的函数 def remove_poor_performance_staff(staff_db): # 先收集所有要解雇的员工姓名,避免遍历字典时修改字典导致运行异常 to_dismiss = [] for name, info in staff_db.items(): # 限定仅处理IT部门、绩效低于50的员工 if info['performance'] < 50 and info['position'] == 'IT': to_dismiss.append(name) # 执行解雇逻辑 for name in to_dismiss: print(f"The employee {name} is recommended for dismissal") del staff_db[name] # 将函数应用到员工库 remove_poor_performance_staff(staff) # 剩余员工按字母顺序排序后输出 sorted_effective = sorted(staff.keys()) print("Effective employees:") for emp in sorted_effective: print(emp)
运行结果
The employee Johnson is recommended for dismissal Effective employees: Ana Jane Joe
内容的提问来源于stack exchange,提问作者user1660475
相关产品推荐
相关产品推荐

