创建一个字典,将员工信息保存到其中,员工工号为键, 其它信息为值; 从字典中获取员工工号为“a4”的员工信息; 遍历字典中所有员工信息,并输出; 更新字典: 将工号为a4的员工的月薪修改为4900; 删除列表中工号为a4的员工数据; 添加一名员工数据:工号a7,姓名李梅,月薪9000。
时间: 2024-03-05 22:49:07 浏览: 149
做一个工资管理系统,要能按照姓名和卡号查找员工的工资信息,以及员工工资信息的插入,删除同时这些信息要存txt
5星 · 资源好评率100%
好的,我可以帮你完成这些操作。
1. 创建一个字典,将员工信息保存到其中,员工工号为键,其它信息为值:
```python
employee_dict = {
"a1": {
"name": "Tom",
"age": 28,
"title": "Engineer",
"department": "IT",
"salary": 5000
},
"a2": {
"name": "Jerry",
"age": 30,
"title": "Manager",
"department": "HR",
"salary": 8000
},
"a3": {
"name": "Lucy",
"age": 25,
"title": "Designer",
"department": "Design",
"salary": 6000
},
"a4": {
"name": "John",
"age": 32,
"title": "Engineer",
"department": "IT",
"salary": 5500
}
}
```
2. 从字典中获取员工工号为“a4”的员工信息:
```python
employee_a4 = employee_dict.get("a4")
print(employee_a4)
```
输出结果:
```
{'name': 'John', 'age': 32, 'title': 'Engineer', 'department': 'IT', 'salary': 5500}
```
3. 遍历字典中所有员工信息,并输出:
```python
for employee_id, employee_info in employee_dict.items():
print(f"Employee ID: {employee_id}")
for key, value in employee_info.items():
print(f"{key}: {value}")
print()
```
输出结果:
```
Employee ID: a1
name: Tom
age: 28
title: Engineer
department: IT
salary: 5000
Employee ID: a2
name: Jerry
age: 30
title: Manager
department: HR
salary: 8000
Employee ID: a3
name: Lucy
age: 25
title: Designer
department: Design
salary: 6000
Employee ID: a4
name: John
age: 32
title: Engineer
department: IT
salary: 5500
```
4. 更新字典:将工号为a4的员工的月薪修改为4900:
```python
employee_dict["a4"]["salary"] = 4900
```
5. 删除字典中工号为a4的员工数据:
```python
del employee_dict["a4"]
```
6. 添加一名员工数据:工号a7,姓名李梅,月薪9000:
```python
employee_dict["a7"] = {
"name": "李梅",
"age": 28,
"title": "Manager",
"department": "HR",
"salary": 9000
}
```
阅读全文