create a function that takes a dictionary and returns a list of int. The list should appear in decreasing order based on the sum of number in each dictionary.
def total_num(dict1):
#Code here
input = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':3}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}}
output = [1,5,3,4,2]
1: 38
2: 5
3: 8
4: 6
5: 15
1>5>3>4>2
def total_num(dict1): items = list(sorted(dict1.items(), key=lambda x: sum(x[1].values()), reverse=True)) result = [] for k, v in items: result.append(k) return result # Testing the function here. ignore/remove the code below if not required input = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar': 11}, 2: {'dos': 2, 'quar': 3}, 3: {'una': 3, 'tres': 5}, 4: {'cin': 6}, 5: {'tres': 7, 'cin': 8}} print(total_num(input))
Get Answers For Free
Most questions answered within 1 hours.