I'm learning Python. This isn't a homework question or anything that's going to be graded. It's an extra question in the chapter covering lists. I completed it with the code below. The total and average points were obviously easy to come up with. I was able to come up with answers for the min and max scoring seasons, but it seems long-winded and kind of hack-y. It seems like there's a better way to come up with those years, but I'm not seeing it with what I've learned so far.
Here is the question given to us:
Complete the following program using functions from the table above to find some statistics
about basketball player Lebron James. The code below provides lists of various statistical
categories for the years 2003-2013. Compute and print the following statistics:
- Total career points
- Average points per game
- Years of the highest and lowest scoring season
Use loops where appropriate.
The lists and commented lines below were provided, I came up with the rest. The min and max scoring seasons work, but I don't like how I did it. Substitute stats from another player, and you'd need to redo all the if-else statements.
# Lebron James: Statistics for 2003/2004 - 2012/2013
games_played = [79, 80, 79, 78, 75, 81, 76, 79, 62, 76]
points = [1654, 2175, 2478, 2132, 2250, 2304, 2258, 2111, 1683, 2036]
assists = [460, 636, 814, 701, 771, 762, 773, 663, 502, 535]
rebounds = [432, 588, 556, 526, 592, 613, 554, 590, 492, 610]
# Print total points
print("Total points:", sum(points))
# Print Average PPG
print("Average points per game:", round((sum(points) / sum(games_played)), 2))
# Print best scoring years (Ex: 2004/2005)
if max(points) == 1654:
max_year = "2003/2004"
elif max(points) == 2175:
max_year = "2004/2005"
elif max(points) == 2478:
max_year = "2005/2006"
elif max(points) == 2132:
max_year = "2006/2007"
elif max(points) == 2250:
max_year = "2007/2008"
elif max(points) == 2304:
max_year = "2008/2009"
elif max(points) == 2258:
max_year = "2009/2010"
elif max(points) == 2111:
max_year = "2010/2011"
elif max(points) == 1683:
max_year = "2011/2012"
else:
max_year = "2012/2013"
print("Highest scoring year: {}".format(max_year))
# Print worst scoring years (Ex: 2004/2005)
if min(points) == 1654:
min_year = "2003/2004"
elif min(points) == 2175:
min_year = "2004/2005"
elif min(points) == 2478:
min_year = "2005/2006"
elif min(points) == 2132:
min_year = "2006/2007"
elif min(points) == 2250:
min_year = "2007/2008"
elif min(points) == 2304:
min_year = "2008/2009"
elif min(points) == 2258:
min_year = "2009/2010"
elif min(points) == 2111:
min_year = "2010/2011"
elif min(points) == 1683:
min_year = "2011/2012"
else:
min_year = "2012/2013"
print("Lowest scoring year: {}".format(min_year))
So, it works. I get the results I'm expecting, but it seems like there's a better way to get to the answer because this is so "static". It seems like having another list with seasons might be helpful, but I'm not sure how to link them together to say points[0] = seasons[0]
or something like that.
Any suggestions?
CodePudding user response:
That's a ridiculous way to do it because of the reasons you identified - this is a good thing! Your Spidey Sense is tingling, telling you there's a better way!
Consider the problem at hand: we have the following data, where the i
th element of each list correspond to each other.
# Lebron James: Statistics for 2003/2004 - 2012/2013
games_played = [79, 80, 79, 78, 75, 81, 76, 79, 62, 76]
points = [1654, 2175, 2478, 2132, 2250, 2304, 2258, 2111, 1683, 2036]
assists = [460, 636, 814, 701, 771, 762, 773, 663, 502, 535]
rebounds = [432, 588, 556, 526, 592, 613, 554, 590, 492, 610]
We want to find the year in which the maximum points were scored. First, let's make a list of years:
years = list(range(2003, 2013))
Now, let's loop over the points and years simultaneously, and keep track of the max points, and which year they were scored in:
max_pts = 0 # The max points scored
max_pts_years = [] # A list containing all years where max points were scored
for pts, year in zip(points, years):
if pts == max_pts:
# If this year's points are equal to max pts,
# add this year to the list
max_pts_years.append(year)
elif pts > max_pts:
# If this year's points are more than max_pts seen so far,
# Set max_pts, and reset the years list to one that contains
# only this year
max_pts = pts
max_pts_years = [year]
At the end of this exercise, you should have the max points, and the years in which they were scored:
print(f"Max points ({max_pts}) were scored in year(s) {max_pts_years}"
# Max points (2478) were scored in year(s) [2005]
If you don't really care about all the years in which the max points were scored, you could simply call the max
function on zip(points, years)
. Since zip
returns tuples containing corresponding elements from each of its arguments, and tuple comparison is done element-wise, you could do:
max_pts, max_pts_year = max(zip(points, years))
print(f"Max points ({max_pts}) were scored in the year {max_pts_year}")
# Max points (2478) were scored in the year 2005
CodePudding user response:
You can easily create a list of years and then use the index of your max and min to return the corresponding year.
# Lebron James: Statistics for 2003/2004 - 2012/2013
games_played = [79, 80, 79, 78, 75, 81, 76, 79, 62, 76]
points = [1654, 2175, 2478, 2132, 2250, 2304, 2258, 2111, 1683, 2036]
assists = [460, 636, 814, 701, 771, 762, 773, 663, 502, 535]
rebounds = [432, 588, 556, 526, 592, 613, 554, 590, 492, 610]
## Create a list of years from 2003 to 2013
years = [year for year in range(2003, 2013)]
# Print total points
print("Total points:", sum(points))
# Print Average PPG
print("Average points per game:", round((sum(points) / sum(games_played)), 2))
# Print best scoring years (Ex: 2004/2005)
### Print the max and min from the points list for confirmation
max_value = max(points)
print(f'Max Points: {max_value}')
min_value = min(points)
print(f'Min Points: {min_value}')
### Get the index of the max and min points
idx_max = points.index(max(points))
idx_min = points.index(min(points))
### Return the year from the years list using the index from points
print(f"Highest scoring year: {years[idx_max]}/{years[idx_max] 1}")
print(f"Lowest scoring year: {years[idx_min]}/{years[idx_min] 1}")