from csv import reader
file=open('hacker_news.csv')
dataset=reader(file)
hn=list(dataset)
print(hn[:5])
print(len(hn))
[['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'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01']] 20101
hn_headers=hn[0]
print(hn_headers)
hn=hn[1:]
print(hn[:5])
['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'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01'], ['10301696', 'Note by Note: The Making of Steinway L1037 (2007)', 'http://www.nytimes.com/2007/11/07/movies/07stein.html?_r=0', '8', '2', 'walterbell', '9/30/2015 4:12']]
ask_posts=[]
show_posts=[]
other_posts=[]
for i in hn:
title=i[1]
if (title.lower()).startswith('ask hn'):
ask_posts.append(i)
elif (title.lower()).startswith('show hn'):
show_posts.append(i)
else:
other_posts.append(i)
len(ask_posts)
print('\n')
len(show_posts)
1162
print(ask_posts[:2])
print('\n')
print(show_posts[:2])
[['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']] [['10627194', 'Show HN: Wio Link ESP8266 Based Web of Things Hardware Development Platform', 'https://iot.seeed.cc', '26', '22', 'kfihihc', '11/25/2015 14:03'], ['10646440', 'Show HN: Something pointless I made', 'http://dn.ht/picklecat/', '747', '102', 'dhotson', '11/29/2015 22:46']]
total_ask_comments=0
length=0
for row in ask_posts:
a=row[4]
total_ask_comments+=int(a)
length+=1
avg_ask_comments=total_ask_comments/length
print(avg_ask_comments)
14.038417431192661
total_show_comments=0
show_length=0
for row in show_posts:
a=row[4]
total_show_comments+=int(a)
show_length+=1
avg_show_comments=total_show_comments/show_length
print(avg_show_comments)
10.31669535283993
import datetime as dt
result_list=[]
for row in ask_posts:
date=row[6]
comments=int(row[4])
result_list.append([date,comments])
#print(result_list)
print(len(ask_posts))
print(len(result_list))
counts_by_hour={}
comments_by_hour={}
total=0
for row in result_list:
date=row[0]
comment=int(row[1])
#print(date)
dt_object=dt.datetime.strptime(date,"%m/%d/%Y %H:%M")
#print(start_date)
hour=dt_object.strftime("%H")
#print(hour)
if hour in counts_by_hour:
counts_by_hour[hour]+=1
comments_by_hour[hour]+=comment
else:
counts_by_hour[hour]=1
comments_by_hour[hour]=comment
total+=comment
print(counts_by_hour)
print(comments_by_hour)
print(total)
1744 1744 {'04': 47, '18': 109, '19': 110, '10': 59, '11': 58, '03': 54, '05': 46, '12': 73, '00': 55, '09': 45, '16': 108, '21': 109, '06': 44, '15': 116, '17': 100, '08': 48, '23': 68, '01': 60, '20': 80, '07': 34, '22': 71, '02': 58, '13': 85, '14': 107} {'04': 337, '18': 1439, '19': 1188, '10': 793, '11': 641, '03': 421, '05': 464, '12': 687, '00': 447, '09': 251, '16': 1814, '21': 1745, '06': 397, '15': 4477, '17': 1146, '08': 492, '23': 543, '01': 683, '20': 1722, '07': 267, '22': 479, '02': 1381, '13': 1253, '14': 1416} 24483
avg_by_hour=[]
for i in comments_by_hour:
avg_by_hour.append([i,comments_by_hour[i]/counts_by_hour[i]])
print(avg_by_hour)
[['04', 7.170212765957447], ['18', 13.20183486238532], ['19', 10.8], ['10', 13.440677966101696], ['11', 11.051724137931034], ['03', 7.796296296296297], ['05', 10.08695652173913], ['12', 9.41095890410959], ['00', 8.127272727272727], ['09', 5.5777777777777775], ['16', 16.796296296296298], ['21', 16.009174311926607], ['06', 9.022727272727273], ['15', 38.5948275862069], ['17', 11.46], ['08', 10.25], ['23', 7.985294117647059], ['01', 11.383333333333333], ['20', 21.525], ['07', 7.852941176470588], ['22', 6.746478873239437], ['02', 23.810344827586206], ['13', 14.741176470588234], ['14', 13.233644859813085]]
swap_avg_by_hour=[]
for i in avg_by_hour:
swap_avg_by_hour.append([i[1],i[0]])
print(swap_avg_by_hour)
sorted_swap=sorted(swap_avg_by_hour, reverse=True)
print("Top 5 hours for Ask posts comments")
for i in sorted_swap[:5]:
print(i)
print('\n')
demo=sorted_swap[0]
#print(demo[1])
date_object=dt.datetime.strptime(demo[1],"%H")
Hour=date_object.strftime("%H:%M")
#print(Hour)
print("{}: {:.2f} average commnets per post".format(Hour,demo[0]))
[[7.170212765957447, '04'], [13.20183486238532, '18'], [10.8, '19'], [13.440677966101696, '10'], [11.051724137931034, '11'], [7.796296296296297, '03'], [10.08695652173913, '05'], [9.41095890410959, '12'], [8.127272727272727, '00'], [5.5777777777777775, '09'], [16.796296296296298, '16'], [16.009174311926607, '21'], [9.022727272727273, '06'], [38.5948275862069, '15'], [11.46, '17'], [10.25, '08'], [7.985294117647059, '23'], [11.383333333333333, '01'], [21.525, '20'], [7.852941176470588, '07'], [6.746478873239437, '22'], [23.810344827586206, '02'], [14.741176470588234, '13'], [13.233644859813085, '14']] Top 5 hours for Ask posts comments [38.5948275862069, '15'] [23.810344827586206, '02'] [21.525, '20'] [16.796296296296298, '16'] [16.009174311926607, '21'] 15:00: 38.59 average commnets per post