Guided Project: Exploring Hacker News Posts step 7 of 8 [ sorted ]

Dear friends,

Hello everyone, this is my first post in community and hope someone could help me!
I follow the guided steps but get the same results at the last step.
I guess the problem starts from “sort()” function but I can’t figure out why.
It would be a great help if someone could point out the problem for me!
Thank you!

Screen Link:
https://app.dataquest.io/m/356/guided-project%3A-exploring-hacker-news-posts/7/sorting-and-printing-values-from-a-list-of-lists

My Code:

## question 1
swap_avg_by_hour = []

for swap in avg_by_hour:
    swap_avg_by_hour.append([swap[1], swap[0]])

# use sorted() function to sort
from operator import itemgetter
sorted_swap = sorted(swap_avg_by_hour, key = itemgetter(0), reverse = True)

print(sorted_swap)

## question 2

for avg, hr in sorted_swap[:5]:
    print(
        "{}:00 {:.2f} average comments per post".format(hr, avg)
        )

What I expected to happen:

What actually happened:

result of question 1

[[38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [38.5948275862069, '15'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [23.810344827586206, '02'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [21.525, '20'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.796296296296298, '16'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [16.009174311926607, '21'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [14.741176470588234, '13'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.440677966101696, '10'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.233644859813085, '14'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [13.20183486238532, '18'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.46, '17'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.383333333333333, '01'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [11.051724137931034, '11'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.8, '19'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.25, '08'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [10.08695652173913, '05'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.41095890410959, '12'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [9.022727272727273, '06'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [8.127272727272727, '00'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.985294117647059, '23'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.852941176470588, '07'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.796296296296297, '03'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [7.170212765957447, '04'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [6.746478873239437, '22'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09'], [5.5777777777777775, '09']]

result of question 2

15:00 38.59 average comments per post
15:00 38.59 average comments per post
15:00 38.59 average comments per post
15:00 38.59 average comments per post
15:00 38.59 average comments per post

Hi @yida0127, welcome to the community!

I copied your code into a copy of my project, but it seemed to work fine. I’m guessing that something went wrong in a previous section of code, probably when you created avg_by_hour. Try inspecting avg_by_hour and see if the values are all different. If you can’t figure it out, it would help if you could upload a copy of your .ipynb file so we can have a look.

1 Like

Hi @april.g, nice to meet you, too!

I upload my copy of .jpynb as below :slight_smile:
yida0127_Hackernews.ipynb (17.3 KB)

Thanks for your reply, after looking back and rewriting avg_by_hour again, the problem seems solved.
However, I still had some questions.
The avg_by_hour I got is a little bit different to result in page 7 of 8, for example:

# the answer should be
['09', 5.5777777777777775],
 ['13', 14.741176470588234],
 ['10', 13.440677966101696],
 ['14', 13.233644859813085],
 ['16', 16.796296296296298],...etc
# my answer is 
['09', 5.4666666666666666],
['13', 14.411764705882353],
['10', 13.440677966101696],
['14', 13.214953271028037],
['16', 16.64814814814815],...etc

I’d like to know if there’s still something wrong?
but it won’t affect the final result as I get answer the same as others.

Thank you!

Click here to view the jupyter notebook file in a new tab

1 Like

Glad you figured it out!

As far as the different results, I noticed that you have 2 code blocks that are really similar (blocks [11] and [12]). When I deleted the 2nd version, the results worked out the same as you’ve seen on other projects. I looked again at it and it looks like in the 2nd version the difference was here:

if hour not in counts_by_hour:
        counts_by_hour[hour] = 1
        comments_by_hour[hour] = 1        # should be = comment
1 Like

Hi @april.g,

ahh!! I didn’t notice I forgot to delete the 2nd version.
After following your steps to correct below, my results worked out!

comments_by_hour[hour] = comment

really appreciate for your help!
hope you have a nice day :slight_smile: