Reputation: 31
I have to create a function called read_data that takes a filename as its only parameter. This function must then open the file with the given name and return a dictionary where the keys are the location names in the file and the values are a list of the readings.
The result of the first function works and displays:
{'Monday': [67 , 43], 'Tuesday': [14, 26], 'Wednesday': [68, 44], ‘Thursday’:[15, 35],’Friday’:[70, 31],’Saturday’;[34, 39],’Sunday’:[22, 18]}
The second function named get_average_dictionary that takes a dictionary structured like the return value of read_data as its only parameter and returns a dictionary with the same keys as the parameter, but with the average value of the readings rather than the list of individual readings. This has to return:
{'Monday': [55.00], 'Tuesday': [20.00], 'Wednesday': [56.00], ‘Thursday’:[25.00],’Friday’:[50.50],’Saturday’;[36.50],’Sunday’:[20.00]}
But I can not get it to work. I get the following errors:
line 25, in <module>
averages = get_average_dictionary(readings)
line 15, in get_average_dictionary
average = {key: sum(val)/len(val) for key, val in readings.items()}
AttributeError: 'NoneType' object has no attribute 'items'
Here is the code I have at the moment. Any help would be appreciated.
def read_data(filename):
readings = {}
with open("c:\\users\\jstew\\documents\\readings.txt") as f:
for line in f:
(key, val) = line.split(',')
if not key in readings.keys():
readings[key] = []
readings[key].append(int(val))
print(readings)
def get_average_dictionary(readings):
average = {key: sum(val)/len(val) for key, val in readings.items()}
print(average)
FILENAME = "readings.txt"
if __name__ == "__main__":
try:
readings = read_data(FILENAME)
averages = get_average_dictionary(readings)
# Loops through the keys in averages, sorted from that with the largest associated value in averages to the lowest - see https://docs.python.org/3.5/library/functions.html#sorted for details
for days in sorted(averages, key = averages.get, reverse = True):
print(days, averages[days])
Upvotes: 0
Views: 2121
Reputation: 3559
You were close but had at least one problem. One was this:
Friday’:[50.50],’Saturday’;[36.50],’Sunday’: [22, 18]
Notice ’Saturday’ is followed by a semicolon, not a colon. That's in both examples. Also, notice your text changes color from red to blue. That usually (this case included) means that you switched from single quotes to something like smartquotes or a character that looks like a normal quote but isn't recognized as such.
{'Monday': [67 , 43], 'Tuesday': [14, 26], 'Wednesday': [68, 44], ‘Thursday’:[15, 35],’Friday’:[70, 31],’Saturday’;[34, 39],’Sunday’:[22, 18]}
Once those are cleared up you just have to deal with the last part, returning vs printing the result.
def get_average_dictionary(readings):
return {k:(sum(v)/len(v)) for (k,v) in vals.items()}
Upvotes: 0
Reputation: 103814
Given:
di={'Monday': [67 , 43], 'Tuesday': [14, 26], 'Wednesday': [68, 44], 'Thursday':[15, 35],'Friday':[70, 31],'Saturday':[34, 39],'Sunday':[22, 18]}
You can do:
>>> {k:sum(v)/len(v) for k,v in di.items()}
{'Monday': 55.0, 'Tuesday': 20.0, 'Wednesday': 56.0, 'Thursday': 25.0, 'Friday': 50.5, 'Saturday': 36.5, 'Sunday': 20.0}
The error you have seems to be that you are returning nothing from your function. Just do:
def a_func(di):
return {k:sum(v)/len(v) for k,v in di.items()}
And you should be good to go...
Upvotes: 1