Exploring Hacker News Posts: Mission 5

Hi fellow glorious Dataquest Learners!
I’m in the Mission 5 of Exploring Hacker News Posts, in the end of Step 5, I probably do it right (generating dictionaries of comments and counts). However, I don’t fully understand what line 8 said about the arguments.

  1. Use the string we want to parse as the first argument and a string that specifies the format as the second argument.

All I did is following code:

for row in result_list:
time = row[0]
date_time = dt.datetime.strptime(time, “%m/%d/%Y %H:%M”)
hour = date_time.strftime("%H")
if hour not in counts_by_hour:
counts_by_hour[hour] = 1
comments_by_hour[hour] = int(row[1])
else:
counts_by_hour[hour] += 1
comments_by_hour[hour] += int(row[1])

Can someone explain it to me, thanks.

counts_by_hour = {} {'13': 85, '14': 107, '15': 116, '20': 80, '12': 73, '23': 68, '11': 58, '21': 109, '03': 54, '16': 108, '19': 110, '18': 109, '08': 48, '07': 34, '22': 71, '00': 55, '04': 47, '01': 60, '05': 46, '09': 45, '02': 58, '10': 59, '17': 100, '06': 44} comments_by_hour = {} {'13': 1253, '14': 1416, '15': 4477, '20': 1722, '12': 687, '23': 543, '11': 641, '21': 1745, '03': 421, '16': 1814, '19': 1188, '18': 1439, '08': 492, '07': 267, '22': 479, '00': 447, '04': 337, '01': 683, '05': 464, '09': 251, '02': 1381, '10': 793, '17': 1146, '06': 397}

Hi there, welcome to the community!

Even though you didn’t fully understand this line in the instructions, you did really well in executing it! :star_struck:

The data provided is in a string format, but we want to take advantage of the datetime module by converting these strings into the datetime format (strptime()) and extracting the data we want (strftime()). The datetime.strptime() function takes 2 arguments: the first one is the string that you want to convert to a datetime object, and the second one is the time format the string is in from our dataset. In your case, the string you want to format is time (from row[0] in result_list) and the format the data is in is "%m/%d/%Y %H:%M".

I hope that helps clear it up! Good luck on the rest of your project.

1 Like