Duplicate result in Hackers news project

Screen Link: Learn data science with Python and R projects

My Code:

print(“Top 5 Hours for Ask Posts Comments”)

for avg, hr in sorted_swap[:5]:

hr = dt.datetime.strptime(hr, "%H").strftime("%H:%M")
print("{}:{:.2f} average comments per post".format(hr,avg)) 

Replace this line with your code


What I expected to happen:
Top 5 Hours for Ask Posts Comments
15:00:38.59 average comments per post
02:00:23.81 average comments per post
20:00:21.52 average comments per post
16:00:16.80 average comments per post
21:00:16.01 average comments per post

What actually happened: 

Top 5 Hours for Ask Posts Comments
15:00:38.59 average comments per post
02:00:23.81 average comments per post
20:00:21.52 average comments per post
16:00:16.80 average comments per post
21:00:16.01 average comments per post
Top 5 Hours for Ask Posts Comments
15:00:38.59 average comments per post
02:00:23.81 average comments per post
20:00:21.52 average comments per post
16:00:16.80 average comments per post
21:00:16.01 average comments per post


it shows the result 2 times. also, why i don't need to write "hr = row[1], avg = row[0]"?

I ran your code and it ran exactly as expected. The only thing I can think of is that it is most likely because of your sorted_swap list (including swap_avg_by_hour). How are you creating those two?

Here’s the code of creating sorted_swap:

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

print(swap_avg_by_hour)
sorted_swap = sorted(swap_avg_by_hour, reverse = True)
print(sorted_swap)

it doesn’t seem it will create result two times…

Sorry, not sure what’s happening here. I tried this code as well and it runs as expected.

Have you tried to run the code again since you posted? Does it produce the same output every time?

I would recommend sharing your Jupyter Notebook here, as a reply to my comment. Follow the steps (except Step 2) listed here - Sharing Your Guided Project in the Community