Hacker News Mission 6

Hi,
I’m currently having trouble with mission 6 of Hacker News project.
My code gives me these results.

avg_by_hour =

for hour in counts_by_hour:
average = comments_by_hour[hour]/counts_by_hour[hour]
avg_by_hour.append([hour, average])
print(avg_by_hour)

[[0, 8.160714285714286], [1, 11.737704918032787], [2, 23.45762711864407], [3, 7.672727272727273], [4, 7.083333333333333], [5, 10.48936170212766], [6, 8.844444444444445], [7, 7.685714285714286], [8, 10.142857142857142], [9, 5.586956521739131], [10, 13.233333333333333], [11, 10.898305084745763], [12, 9.337837837837839], [13, 14.906976744186046], [14, 13.13888888888889], [15, 38.27350427350427], [16, 16.798165137614678], [17, 11.356435643564357], [18, 13.1], [19, 10.72972972972973], [20, 21.28395061728395], [21, 15.9], [22, 6.680555555555555], [23, 7.884057971014493]]

But the results I should be seeing is this apparently:
[
[‘09’, 5.5777777777777775],
[‘13’, 14.741176470588234],
[‘10’, 13.440677966101696],
[‘14’, 13.233644859813085],
[‘16’, 16.796296296296298],
[‘23’, 7.985294117647059],
[‘12’, 9.41095890410959],
[‘17’, 11.46],
[‘15’, 38.5948275862069],
[‘21’, 16.009174311926607],
[‘20’, 21.525],
[‘02’, 23.810344827586206],
[‘18’, 13.20183486238532],
[‘03’, 7.796296296296297],
[‘05’, 10.08695652173913],
[‘19’, 10.8],
[‘01’, 11.383333333333333],
[‘22’, 6.746478873239437],
[‘08’, 10.25],
[‘04’, 7.170212765957447],
[‘00’, 8.127272727272727],
[‘06’, 9.022727272727273],
[‘07’, 7.852941176470588],
[‘11’, 11.051724137931034]
]

I don’t understand why mine has slight differences in values and the order?? Any help is appreciated

for context this was my mission 5 code that I’m using to complete mission 6

import datetime as dt
result_list =

for row in ask_posts:
created_at = row[6]
num_of_comments = int(row[4])
result_list.append([created_at, num_of_comments])

counts_by_hour = {}
comments_by_hour = {}

for row in result_list:
date = row[0] #m/d/yyyy H:M
formated_date = dt.datetime.strptime(date, “%m/%d/%Y %H:%M”)
hour = formated_date.hour
if hour not in counts_by_hour:
counts_by_hour[hour] = 1
comments_by_hour[hour] = row[1]
if hour in counts_by_hour:
counts_by_hour[hour] += 1
comments_by_hour[hour] += row[1]

The good news is, I don’t think you’re doing anything wrong. :slight_smile:

The reason the results are in a different order has to do with the dictionary counts_by_hour. Since dictionaries are not ordered, the order that our list comes out will result from whatever order the dictionary happened to be in at the time the code was run. This isn’t a problem, so don’t worry about the order of the list items – it’s the content that matters. I’m sure there’s ways to make the list ordered the way we like if we do some research. :wink:

The solution code used datetime.strftime() to get the hour from the datetime object. This results in a string value. When you used formated_date.hour, the result is always an integer. This doesn’t seem to be a problem when the goal is to find the average per hour, it just looks a little different.

Regarding the actual values, that is something I can’t explain based on the information here. When I used your code in a copy of my project, my numbers matched what you saw in the solution code. My guess is that there is some difference created earlier in the code so that perhaps ask_posts isn’t the same length as the solution’s result. However, your values are very close, so whatever difference there may be is very minimal.