In: Computer Science
(Python)
I want to use a function called level() that takes a dictionary. Here is a dictionary with people's job and skill level. dict1 = {'Jame': {'Cleaning': 5, 'Tutoring': 2, 'Baking': 1},Pam': {'Plumbing': 2, 'Cleaning': 5}) like if I called level(dict1), the output will return {'Pam', 'James'} It finds the people's average skill level like for Pam is (2+5)/2=3.5 and sorted descending. How do I do that and how do I do it in only one return statement(using comprehension or others)?
Code :
def level(dict):
#dictionary to maintain avgskill of each person
avglevel = {}
#traversing each person
for i in dict:
avgskill = 0
#traversing each skill of person
for j in dict[i]:
avgskill = avgskill + dict[i][j]
avgskill = avgskill/len(dict[i])
#adding values to dictionary
avglevel[i] = avgskill
#sorting in descending order
sorted_avglevel = sorted(avglevel, key=avglevel.get,reverse=True)
return sorted_avglevel
dict1 = {'James': {'Cleaning': 5, 'Tutoring': 2, 'Baking': 1},'Pam': {'Plumbing': 2, 'Cleaning': 5}}
print(level(dict1))
output: