-3

如何调整这本词典中的某些数据,我只能显示它们,但不能选择值,例如员工中表现最差的。

  staff = {
    'Williams': {
        'position': 'marketing',
        'performance': 71
    },
    'Kelly': {
        'position': 'marketing',
        'performance': 65
    },
    'Johnson': {
        'position': 'marketing',
        'performance': 49
    },
    'Thompson': {
        'position': 'marketing',
        'performance': 53
    }
}

for key, value in staff.items():
        print('The employee', key, 'is recommended for dismissal')

        for k, v in value.items():
                 print(v)

这是输出:

The employee Williams is recommended for dismissal
marketing
71
The employee Kelly is recommended for dismissal
marketing
65
The employee Johnson is recommended for dismissal
marketing
49
The employee Thompson is recommended for dismissal
marketing
53
4

2 回答 2

0

要从员工中获得最低绩效者,您可以使用以下代码:

sorted(staff.items(), key=lambda item: item[1]['performance'])[0]

它按 对字典进行排序performance并显示第一个结果

于 2021-08-03T07:46:38.077 回答
0

您始终可以编写代码来执行此操作,但是没有函数可以执行此操作。您可以创建另一个字典来保存最低、最高或您想要的项目。

于 2021-08-03T07:45:30.383 回答