请编写程序找出该公司中薪水最高的职员,并打印出他的姓名。 (30分) 请将程序写入到下面的second()函数中,写在函数体之外无效。 employees = [ {'name':'张无忌', 'department': '研发部', 'salary': 9896.45}, {'name':'杨过', 'department': '研发部', 'salary': 28136.39}, {'name':'小龙女', 'department': '市场部', 'salary': 7424.28}, {'name':'乔峰', 'department': '研发部', 'salary': 57636.89}, {'name':'阿碧', 'department': '市场部', 'salary': 4748.79}, ] """ def second(): pass """
时间: 2023-08-06 13:21:56 浏览: 132
可以使用 sorted 函数,结合 lambda 表达式对列表进行排序,然后取出第一个元素即可。代码如下:
```python
def second():
employees = [
{'name': '张无忌', 'department': '研发部', 'salary': 9896.45},
{'name': '杨过', 'department': '研发部', 'salary': 28136.39},
{'name': '小龙女', 'department': '市场部', 'salary': 7424.28},
{'name': '乔峰', 'department': '研发部', 'salary': 57636.89},
{'name': '阿碧', 'department': '市场部', 'salary': 4748.79},
]
max_salary_employee = sorted(employees, key=lambda x: x['salary'], reverse=True)[0]['name']
print('薪水最高的职员是:', max_salary_employee)
```
输出结果为:
```
薪水最高的职员是: 乔峰
```
相关问题
编程题:有一个列表,该列表中存储了某个公司的职员的信息, 13 请编写程序找出该公司中薪水最高的职员,并打印出他的姓名。(30分) 14 请将程序写入到下面的second O)函数中,写在函数体之外无效。 15 employees -[ 16 'name':'张无忌','department':"研发部', ' salary':9896.45}, 17 'name':'杨过',"department':‘研发部','salary':28136.39], 18 F'name ':'小龙女!,'depar tment':'市场部','salary':7424.28}, 19 {'name':"乔峰!,' department!:"研发部','salary ':57636.89], 20 'name ':'阿碧',department':"市场部',' salary':4748.79], 21 ] 22 1I111 second0 Packages TOD0Python Console!ProblemsTerminalServices 24
可以使用以下代码实现:
```python
def secondO():
employees = [
{'name': '张无忌', 'department': '研发部', 'salary': 9896.45},
{'name': '杨过', 'department': '研发部', 'salary': 28136.39},
{'name': '小龙女', 'department': '市场部', 'salary': 7424.28},
{'name': '乔峰', 'department': '研发部', 'salary': 57636.89},
{'name': '阿碧', 'department': '市场部', 'salary': 4748.79}
]
max_salary = 0
max_name = ''
for employee in employees:
if employee['salary'] > max_salary:
max_salary = employee['salary']
max_name = employee['name']
print('薪水最高的职员是:{}'.format(max_name))
secondO()
```
输出结果为:
```
薪水最高的职员是:乔峰
```
阅读全文