Guided Project - Exploring Hacker News Posts - STEP 7 - SORTED SWAP Error

Screen Link:
Python Project: Exploring Hacker News Posts | Dataquest

My Code:

from csv import reader
import datetime as dt
open_file = open("hacker_news.csv")
read_file = reader(open_file)
hn = list(read_file)
headers = hn[0]
hn = hn[1:]
def explore_data(dataset,start,finish,col_rows=True):

    dataset_slice = dataset[start:finish]
    for row in dataset_slice:
        print(row)
        print('\n')

    if col_rows:
        print('rows:' , len(dataset))
        print('columns:' , len(dataset[0]))
    
print(headers)
print('\n')
explore_data(hn,0,5,True)
['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 ■■■■ Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-■■■■-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']


rows: 20100
columns: 7
ask_posts = []
show_posts = []
other_posts = []

for row in hn:
    title = row[1]
    if title.lower().startswith("ask hn"):
        ask_posts.append(row)
    elif title.lower().startswith("show hn"):
        show_posts.append(row)
    else:
        other_posts.append(row)

print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
print(len(ask_posts)+len(show_posts)+len(other_posts))
1744
1162
17194
20100
total_ask_comments = 0
total_show_comments = 0
total = 0

for row in ask_posts:
    total += 1
    num_comments = int(row[4])
    total_ask_comments += num_comments

avg_ask_comments = total_ask_comments/total
print(avg_ask_comments)

for row in show_posts:
    total += 1
    num_comments = int(row[4])
    total_show_comments += num_comments
    
avg_show_comments = total_show_comments/total
print(avg_show_comments)
    
14.038417431192661
4.125258086717137
result_list = []

for row in ask_posts:
    created_at = row[6]
    num_comments = int(row[4])
    result_list.append([created_at,num_comments])

print(result_list[0])
['8/16/2016 9:55', 6]
date_time = "08/16/2021 09:30"
date_time = dt.datetime.strptime(date_time,'%m/%d/%Y %H:%M')
date_time = date_time.strftime('%H')
print(date_time)
09
counts_by_hour = {}
comments_by_hour = {}

for row in result_list:
        comment = row[1]
        date_time = row[0]
        date_time = dt.datetime.strptime(date_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] = comment
        else:
            counts_by_hour[hour] += 1
            comments_by_hour[hour] += comment

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

for hour in counts_by_hour:
    if hour in comments_by_hour:
        avg = round(comments_by_hour[hour]/counts_by_hour[hour],2)
        avg_by_hour.append([hour,avg])

print(avg_by_hour)
   
[['09', 5.58], ['13', 14.74], ['10', 13.44], ['14', 13.23], ['16', 16.8], ['23', 7.99], ['12', 9.41], ['17', 11.46], ['15', 38.59], ['21', 16.01], ['20', 21.52], ['02', 23.81], ['18', 13.2], ['03', 7.8], ['05', 10.09], ['19', 10.8], ['01', 11.38], ['22', 6.75], ['08', 10.25], ['04', 7.17], ['00', 8.13], ['06', 9.02], ['07', 7.85], ['11', 11.05]]
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)
[[5.58, '09'], [14.74, '13'], [13.44, '10'], [13.23, '14'], [16.8, '16'], [7.99, '23'], [9.41, '12'], [11.46, '17'], [38.59, '15'], [16.01, '21'], [21.52, '20'], [23.81, '02'], [13.2, '18'], [7.8, '03'], [10.09, '05'], [10.8, '19'], [11.38, '01'], [6.75, '22'], [10.25, '08'], [7.17, '04'], [8.13, '00'], [9.02, '06'], [7.85, '07'], [11.05, '11']]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-307863a4b1cd> in <module>
      6 print(swap_avg_by_hour)
      7 
----> 8 sorted_swap = sorted(swap_avg_by_hour, reverse=True)
      9 
     10 print(sorted_swap)

TypeError: 'list' object is not callable

What I expected to happen:

What actually happened:
I am not sure why I am getting the “list object is not callable” error.

TypeError: 'list' object is not callable

Please help.

I tried even copying and pasting from the solutions and I still get this error. I am assuming this means that the error is further up in the code, but I have not been able to figure it out. TIA!!

Are you running this code on your system or on DQ’s platform?

I ran your code above and didn’t get any error.

1 Like

Hi doctor, running it on DQ’s platform. I re-ran it now and it works! Strange. Thanks for the feedback! Appreciate it. Can finally move on from that step.