from csv import reader
opened_file=open("hacker_news.csv")
read_file=reader(opened_file)
hn=list(read_file)
hn[:3]
[['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at'], ['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30']]
headers=hn[0]
hn=hn[1:]
print(headers)
hn[:3]
['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at']
[['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20']]
ask_posts, show_posts, other_posts=[], [], []
for item in hn:
title=item[1]
if title.lower().startswith("ask hn"):
ask_posts.append(item)
elif title.lower().startswith("show hn"):
show_posts.append(item)
else:
other_posts.append(item)
ask_posts[:5]
[['12296411', 'Ask HN: How to improve my personal website?', '', '2', '6', 'ahmedbaracat', '8/16/2016 9:55'], ['10610020', 'Ask HN: Am I the only one outraged by Twitter shutting down share counts?', '', '28', '29', 'tkfx', '11/22/2015 13:43'], ['11610310', 'Ask HN: Aby recent changes to CSS that broke mobile?', '', '1', '1', 'polskibus', '5/2/2016 10:14'], ['12210105', 'Ask HN: Looking for Employee #3 How do I do it?', '', '1', '3', 'sph130', '8/2/2016 14:20'], ['10394168', 'Ask HN: Someone offered to buy my browser extension from me. What now?', '', '28', '17', 'roykolak', '10/15/2015 16:38']]
total_ask_comments=0
for item in ask_posts:
total_ask_comments+=int(item[4])
total_ask_comments
24483
avg_ask_comments=total_ask_comments/len(ask_posts)
avg_ask_comments
14.038417431192661
total_show_comments=0
for item in show_posts:
total_show_comments+=int(item[4])
total_show_comments
11988
avg_show_comments=total_show_comments/len(show_posts)
avg_show_comments
10.31669535283993
import datetime as dt
print("done")
done
result_list=[]
for i in ask_posts:
result_list.append([i[6],i[4]])
result_list[:5]
[['8/16/2016 9:55', '6'], ['11/22/2015 13:43', '29'], ['5/2/2016 10:14', '1'], ['8/2/2016 14:20', '3'], ['10/15/2015 16:38', '17']]
counts_by_hour, comments_by_hour={},{}
for item in result_list:
post_date=dt.datetime.strptime(item[0],"%m/%d/%Y %H:%M")
post_hour=post_date.strftime("%H")
#dt.datetime.strftime(post_date,"%H")
if post_hour not in counts_by_hour:
counts_by_hour[post_hour]=1
comments_by_hour[post_hour]=int(item[1])
else:
counts_by_hour[post_hour]+=1
comments_by_hour[post_hour]+=int(item[1])
print(counts_by_hour,"\n\n\n")
print(comments_by_hour)
{'12': 73, '21': 109, '13': 85, '11': 58, '19': 110, '05': 46, '07': 34, '09': 45, '02': 58, '16': 108, '20': 80, '18': 109, '03': 54, '14': 107, '15': 116, '23': 68, '01': 60, '00': 55, '06': 44, '17': 100, '04': 47, '08': 48, '22': 71, '10': 59} {'12': 687, '21': 1745, '13': 1253, '11': 641, '19': 1188, '05': 464, '07': 267, '09': 251, '02': 1381, '16': 1814, '20': 1722, '18': 1439, '03': 421, '14': 1416, '15': 4477, '23': 543, '01': 683, '00': 447, '06': 397, '17': 1146, '04': 337, '08': 492, '22': 479, '10': 793}
t=dt.datetime.strptime(result_list[0][0],"%m/%d/%Y %H:%M")
t
datetime.datetime(2016, 8, 16, 9, 55)
avg_by_hour=[]
for item in comments_by_hour:
avg_by_hour.append([item,comments_by_hour[item]/counts_by_hour[item]])
avg_by_hour
[['12', 9.41095890410959], ['21', 16.009174311926607], ['13', 14.741176470588234], ['11', 11.051724137931034], ['19', 10.8], ['05', 10.08695652173913], ['07', 7.852941176470588], ['09', 5.5777777777777775], ['02', 23.810344827586206], ['16', 16.796296296296298], ['20', 21.525], ['18', 13.20183486238532], ['03', 7.796296296296297], ['14', 13.233644859813085], ['15', 38.5948275862069], ['23', 7.985294117647059], ['01', 11.383333333333333], ['00', 8.127272727272727], ['06', 9.022727272727273], ['17', 11.46], ['04', 7.170212765957447], ['08', 10.25], ['22', 6.746478873239437], ['10', 13.440677966101696]]
a={'12:30': 21}
b={'12:30': 7}
print(a.keys()==b.keys())
True
swap_avg_by_hour=[]
for i in avg_by_hour:
swap_avg_by_hour.append([i[1],i[0]])
swap_avg_by_hour[:5]
[[9.41095890410959, '12'], [16.009174311926607, '21'], [14.741176470588234, '13'], [11.051724137931034, '11'], [10.8, '19']]
sorted_swap=sorted(swap_avg_by_hour)
sorted_swap
[[5.5777777777777775, '09'], [6.746478873239437, '22'], [7.170212765957447, '04'], [7.796296296296297, '03'], [7.852941176470588, '07'], [7.985294117647059, '23'], [8.127272727272727, '00'], [9.022727272727273, '06'], [9.41095890410959, '12'], [10.08695652173913, '05'], [10.25, '08'], [10.8, '19'], [11.051724137931034, '11'], [11.383333333333333, '01'], [11.46, '17'], [13.20183486238532, '18'], [13.233644859813085, '14'], [13.440677966101696, '10'], [14.741176470588234, '13'], [16.009174311926607, '21'], [16.796296296296298, '16'], [21.525, '20'], [23.810344827586206, '02'], [38.5948275862069, '15']]
Top 5 Hours for Ask Posts Comments
for i in sorted_swap[-5:]:
print("{} {} average comments per post".format(dt.datetime.strptime(i[1],"%H").strftime("%H:%M"),round(i[0],2)))
21:00 16.01 average comments per post 16:00 16.8 average comments per post 20:00 21.52 average comments per post 02:00 23.81 average comments per post 15:00 38.59 average comments per post
round(16.009174311926607,2)
16.01