Reputation: 143
I have a nested list like so:
temperature = [["Jan", 12, 18, 16, 18], ["Feb", 10, 20, 50, 50], ["Mar", 23, 32, 10, 32]]
the list contains all the months up until December with the 3 highest temp recorded over the entire month and then the highest out of the 3 appended to the end of the list.
I have worked out the highest temp for each month in the following way:
def sortTemp():
for temp in temperatures:
temp = [temp[0], temp[4]]
highest.append(temp)
highest.sort()
print(highest)
This works, however I had to work out the max values beforehand, and append it to the temperature list as explained above. Is there an easier way to do this?
Also, how do I work out the average temp of each month using the data provided in the temperature list, sorted by the highest average?
Output should be like:
AverageTemp = [["Month", AverageTemp], ["Month", AverageTemp]]
Upvotes: 2
Views: 205
Reputation: 739
Understanding more about list comprehension and slice notation will really help you do this simply with pythonic code.
To explain @CoryKramer's first answer:
>>> AverageTemp = [[i[0], sum(i[1:4])/len(i[1:4])] for i in temperature]
>>> AverageTemp
[['Jan', 15], ['Feb', 26], ['Mar', 21]]
What he is doing is using i to cycle through the lists in temperature (which is a list of lists) and then using slice notation to get the info you need. So, obviously i[0]
is your month, but the notation sum(i[1:4])
gets the sum of the items in the list from index 1 to the 3 using slice notation. He then divides by the length and creates the list of lists AverageTemp with the information you need.
Note: I have edited his answer to not include the high temperature twice per the comment from @TigerhawkT3.
Upvotes: 0
Reputation: 49310
That's not a very good structure for the given data: you have differentiated items (name of a month, temperatures, maximum temperature) as undifferentiated list elements. I would recommend dictionaries or objects.
months = {"Jan":[12, 18, 16], "Feb":[10, 20, 50], "Mar":[23, 32, 10]}
for month in sorted(months, key=lambda x: sum(months.get(x))/len(months.get(x)), reverse=True):
temps = months.get(month)
print(month, max(temps), sum(temps)/len(temps))
Result:
Feb 50 26.666666666666668
Mar 32 21.666666666666668
Jan 18 15.333333333333334
class Month:
def __init__(self, name, *temps):
self.name = name
self.temps = temps
def average(self):
return sum(self.temps)/len(self.temps)
def maximum(self):
return max(self.temps)
months = [Month('Jan', 12, 18, 16), Month('Feb', 10, 20, 50), Month('Mar', 23, 32, 10)]
for month in sorted(months, key=lambda x: x.average(), reverse=True):
print(month.name, month.maximum(), month.average())
Result:
Feb 50 26.666666666666668
Mar 32 21.666666666666668
Jan 18 15.333333333333334
Upvotes: 0
Reputation: 107297
You can use a list comprehension within sorted
function :
>>> from operator import itemgetter
>>> sorted([[l[0],sum(l[1:])/4]for l in temperature],key=itemgetter(1),reverse=True)
[['Feb', 32], ['Mar', 24], ['Jan', 16]]
In preceding code you first loop over your list then get the first element and the avg of the rest by [l[0],sum(l[1:])/4]
and use operator.itemgetter
as the key of your sorted function to sort your result based on the second item (the average)
Upvotes: 1
Reputation: 117886
You can use a list comprehension to compute the averages
>>> AverageTemp = [[i[0], sum(i[1:])/len(i[1:])] for i in temperature]
>>> AverageTemp
[['Jan', 16.0], ['Feb', 32.5], ['Mar', 24.25]]
Or if you have numpy
>>> import numpy as np
>>> AverageTemp = [[i[0], np.mean(i[1:])] for i in temperature]
>>> AverageTemp
[['Jan', 16.0], ['Feb', 32.5], ['Mar', 24.25]]
Then to sort you can use the key
and reverse
arguments
>>> AverageTemp = sorted([[i[0], np.mean(i[1:])] for i in temperature], key = lambda i: i[1], reverse = True)
>>> AverageTemp
[['Feb', 32.5], ['Mar', 24.25], ['Jan', 16.0]]
Upvotes: 2