In the below dictionary, how would I print the highest overall score?
scores = {
'Monday': [21, 23, 24, 19],
'Tuesday': [16, 15, 12, 19],
'Wednesday': [23, 22, 23],
'Thursday': [18, 20, 26, 24],
'Friday': [17, 22],
'Saturday': [22, 24],
'Sunday': [21, 21, 28, 25]
}
I am very new to python and I didn't even know where to start with this.
CodePudding user response:
You can use max
built-in passing the dictionary items, and a key function which will calculate the sum of the values in the list:
>>> max(scores.items(), key=lambda x:sum(x[1]))
('Sunday', [21, 21, 28, 25])
Or if you need the maximum score instead of the list of the score values:
>>> max(map(lambda x:(x[0], sum(x[1])), scores.items()), key=lambda x:x[1])
('Sunday', 95)
CodePudding user response:
This should do the trick for you.
print(max([max(scores[day]) for day in scores]))
What this is doing is getting the max of all the scores of every day and then getting the overall maximum out of all the maximumns.
To answer your question about accessing item in a list within a dictionary key-value pair, you would first need to access one such pair first:
d = {'a': [1,2,3], 'b': [4,5,6]}
b = d['b']
and then let's say you want to access the 3rd item in the list:
# for 3rd item you'd do 3-1
print(b[2])
# 6 is printed
CodePudding user response:
Here's another approach. What I would do is transform the original data structure to include both the scores and the sum of those scores. You can include other stats that you might find valuable in the future. Then I would apply a second operation to that result to obtain the final result you're looking for. You can get multiple results without duplicating effort.
Is is unclear to me if you want the maximum single score, or if you want the maximum total of all of the scores for a particular day. The approach I explain above will let you easily answer both of those questions.
So here's how to produce a new structure with various statistics about the data:
from pprint import pprint
scores = {
'Monday': [21, 23, 24, 19],
'Tuesday': [16, 15, 12, 19],
'Wednesday': [23, 22, 23],
'Thursday': [18, 20, 26, 24],
'Friday': [17, 22],
'Saturday': [22, 24],
'Sunday': [21, 21, 28, 25]
}
scores = {k: {'scores': v, 'total': sum(v), 'max': max(v), 'min': min(v)} for k, v in scores.items()}
pprint(scores)
Result:
{'Friday': {'max': 22, 'min': 17, 'scores': [17, 22], 'total': 39},
'Monday': {'max': 24, 'min': 19, 'scores': [21, 23, 24, 19], 'total': 87},
'Saturday': {'max': 24, 'min': 22, 'scores': [22, 24], 'total': 46},
'Sunday': {'max': 28, 'min': 21, 'scores': [21, 21, 28, 25], 'total': 95},
'Thursday': {'max': 26, 'min': 18, 'scores': [18, 20, 26, 24], 'total': 88},
'Tuesday': {'max': 19, 'min': 12, 'scores': [16, 15, 12, 19], 'total': 62},
'Wednesday': {'max': 23, 'min': 22, 'scores': [23, 22, 23], 'total': 68}}
Then, if you want to know what day had the largest total of its scores, and what that total was, you can just do this:
print(max(scores.items(), key=lambda x: x[1]['total']))
Which gives you:
('Sunday', {'scores': [21, 21, 28, 25], 'total': 95, 'max': 28, 'min': 21})
If you want to know what day the maximum single score occurred on, you do this instead:
print(max(scores.items(), key=lambda x: x[1]['max']))
Which happens to give you the same result, as Sunday had both the largest single score and the largest total of all scores for the day.
If you had a very large data set, you could avoid the hit of walking through the data three times to compute max
, min
and sum
by writing a small function that would walk over the data and produce those same statistics in a single pass.