so, it displays what the output of step 6 should be; a variety of average comments by hour.
my code instead displayed .5 for all averages, which is startlingly wrong.
Basics (1).ipynb (10.2 KB)
edit: so after i had the total comments per category printed, i should be distributing out 24000 comments, averaging 1000 comments per hour.
but both count by hour and count by post are getting a couple hundred
def com_count(post_section):
total_comments = 0
for row in post_section:
com_count = int(row[4])
total_comments += com_count
avg_comments = round(total_comments / len(post_section),4)
print(avg_comments)
print(total_comments)
output;
The average and total number of comments on Ask HN posts is:
14.0315
24485
The average and total number of comments on Show HN posts is:
10.3021
12002
import datetime as dt
result_list = []
for row in ask_posts:
created_at = row[6]
comments = int(row[4])
result_list.append([created_at, comments])
counts_by_hour = {}
coms_by_hour = {}
for row in result_list:
dt_string= row[0]
#m/d/yyyy 24:mm
dt_object = dt.datetime.strptime(dt_string, '%m/%d/%Y %H:%M')
post_hour = dt_object.strftime('%H')
if post_hour not in counts_by_hour:
counts_by_hour[post_hour] = 1
coms_by_hour[post_hour] = comments
else:
counts_by_hour[post_hour] += 1
coms_by_hour[post_hour] += comments
print(counts_by_hour)
print(lb)
print(coms_by_hour)
{β06β: 44, β18β: 109, β05β: 46, β11β: 58, β23β: 69, β21β: 109, β09β: 45, β14β: 107, β20β: 80, β10β: 59, β08β: 48, β15β: 116, β13β: 85, β12β: 73, β02β: 58, β01β: 60, β16β: 108, β17β: 100, β07β: 34, β22β: 71, β19β: 110, β00β: 55, β03β: 54, β04β: 47}
{β06β: 88, β18β: 218, β05β: 92, β11β: 116, β23β: 138, β21β: 218, β09β: 90, β14β: 214, β20β: 160, β10β: 118, β08β: 96, β15β: 232, β13β: 170, β12β: 146, β02β: 116, β01β: 120, β16β: 216, β17β: 200, β07β: 68, β22β: 142, β19β: 220, β00β: 110, β03β: 108, β04β: 94}
so the total comments accounted for total to 3490, out of the desired 24,485