【问题标题】:Cannot use SORTED for a list不能将 SORTED 用于列表
【发布时间】:2021-07-26 04:54:31
【问题描述】:

当我尝试使用 SORTED 对列表进行排序时,此代码的最后一步出现错误。我收到“列表对象不可调用”的错误消息。我想为此使用 SORTED 而不是 SORT 函数。

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]))
    
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)


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)
    
result_list = []

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

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

              
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])
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)

---------------------------------------------------------------------------
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

不确定如何上传 csv,因为没有看到上传选项。代码中是否存在明显错误?或者有人可以帮助说明上传 csv 文件吗?

【问题讨论】:

    标签: list sorting typeerror


    【解决方案1】:

    您需要在添加 avg_by_hour = [] swap_avg_by_hour= []之前声明这两个列表

    【讨论】:

    • 感谢您查看 :) 我声明了这两个,但列表 ["list object is not callable"] 的类型错误仍然存​​在。您是否注意到其他可能是问题的地方?
    • 将列表swap_avg_by_hour的值添加到问题中
    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 2021-07-16
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    相关资源
    最近更新 更多