容器综合训练

原始数据

# # 员工字典(员工编号 部门编号 姓名 工资)
dict_employees = {
    1001: {"did": 9002, "name": "师父", "money": 60000},
    1002: {"did": 9001, "name": "孙悟空", "money": 50000},
    1003: {"did": 9002, "name": "猪八戒", "money": 20000},
    1004: {"did": 9001, "name": "沙僧", "money": 30000},
    1005: {"did": 9001, "name": "小白龙", "money": 15000},
}
# 部门列表
list_departments = [
    {"did": 9002, "title": "销售部"},
    {"did": 9001, "title": "教学部"},
    {"did": 9003, "title": "品保部"},
]

需求

1. 打印所有员工信息

格式:xx的员工编号是xx,部门编号是xx,月薪xx元.

for employee in dict_employees:
    print("%s的员工编号是%s,部门编号是%s,月薪%s元." % (dict_employees[employee]["name"],employee,dict_employees[employee]["did"],dict_employees[employee]["money"]))
"""
输出结果:
师父的员工编号是1001,部门编号是9002,月薪60000元.
孙悟空的员工编号是1002,部门编号是9001,月薪50000元.
猪八戒的员工编号是1003,部门编号是9002,月薪20000元.
沙僧的员工编号是1004,部门编号是9001,月薪30000元.
小白龙的员工编号是1005,部门编号是9001,月薪15000元.
"""

2. 打印所有月薪大于2w的员工信息

格式:xx的员工编号是xx,部门编号是xx,月薪xx元.

for employee in dict_employees:
    if dict_employees[employee]["money"] > 20000:
        print("%s的员工编号是%s,部门编号是%s,月薪%s元." % (dict_employees[employee]["name"],employee,dict_employees[employee]["did"],dict_employees[employee]["money"]))
"""
输出结果:
师父的员工编号是1001,部门编号是9002,月薪60000元.
孙悟空的员工编号是1002,部门编号是9001,月薪50000元.
沙僧的员工编号是1004,部门编号是9001,月薪30000元.
"""

3. 在部门列表中查找编号最小的部门

min_department = list_departments[0]
for department in list_departments:
    if min_department["did"] > department["did"]:
        min_department = department
print(min_department["title"])
"""
输出结果:
教学部
"""

4. 根据部门编号对部门列表升序排列

for r in range(len(list_departments)-1): 
    # 依次取出该数据的后面的所有数据
    for c in range(r+1,len(list_departments)): 
        #作比较
        if list_departments[r]["did"] >  list_departments[c]["did"]: 
            # 始终保持第一个位置的数为最小,以此类推
            list_departments[r]["did"],list_departments[c]["did"] = list_departments[c]["did"],list_departments[r]["did"]
print(list_departments)
"""
输出结果:
[{'did': 9001, 'title': '销售部'}, {'did': 9002, 'title': '教学部'}, {'did': 9003, 'title': '品保部'}]
"""

 5. (选做)在列表中删除所有偶数

# 错误1:
list01 = [10,20,33,40,51,60,70,80]
for elem in list01:
    if elem % 2 == 0:
        list01.remove(elem)
print(list01)
"""
输出结果:
[20, 33, 51, 70]
"""

# 错误2:
for i in range(len(list01)):
    if list01[i] % 2 == 0:
        del list01[i]
print(list01)
"""
输出结果:
IndexError: list index out of range
"""

# 正确
for i in range(len(list01)-1,-1,-1):
    if list01[i] % 2 == 0:
        del list01[i]
print(list01)
"""
输出结果:
[33, 51]
"""
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值