Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 不能对列表使用排序_List_Sorting_Typeerror - Fatal编程技术网

List 不能对列表使用排序

List 不能对列表使用排序,list,sorting,typeerror,List,Sorting,Typeerror,当我尝试使用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 e

当我尝试使用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导入读取器的

将日期时间导入为dt
open_file=open(“hacker_news.csv”)
读取文件=读取器(打开文件)
hn=列表(读取文件)
标题=hn[0]
hn=hn[1:]
def explore_数据(数据集、开始、结束、列行=True):
数据集\u切片=数据集[开始:完成]
对于数据集_切片中的行:
打印(行)
打印(“\n”)
如果列为列:
打印('行:',列(数据集))
打印('列:',len(数据集[0]))
ask_posts=[]
显示文章=[]
其他职位=[]
对于hn中的行:
标题=第[1]行
如果title.lower().startswith(“ask hn”):
ask_posts.append(行)
elif title.lower().startswith(“show hn”):
显示_posts.append(行)
其他:
其他_post.append(行)
总询问意见=0
显示注释总数=0
总数=0
对于ask_帖子中的行:
总数+=1
num_comments=int(第[4]行)
总评论数+=评论数
平均询问意见=总计询问意见/总计
打印(平均询问意见)
对于show_posts中的行:
总数+=1
num_comments=int(第[4]行)
显示注释总数+=注释数
平均显示注释=总计显示注释/总计
打印(平均显示注释)
结果_列表=[]
对于ask_帖子中的行:
在=行[6]处创建
num_comments=int(第[4]行)
结果\u列表.append([created\u at,num\u comments])
按小时计数={}
注释按小时={}
对于结果列表中的行:
注释=行[1]
日期\时间=行[0]
日期时间=dt.datetime.strtime(日期时间,'%m/%d/%Y%H:%m')
小时=日期\时间.strftime(“%H”)
如果小时不在按小时计数中:
按小时计数[小时]=1
注释按小时[小时]=注释
其他:
按小时计数[小时]+=1
注释按小时[小时]+=注释
按小时计数的小时数:
如果注释中的小时按小时计算:
平均值=四舍五入(按小时[小时]进行评论/按小时[小时]进行计数,2)
按小时平均。追加([小时,平均])
按小时计算的平均行数:
按小时交换平均值。追加([第[1]行,第[0]行])
打印(按小时交换平均值)
已排序的交换=已排序(按小时交换平均值,反向=真)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
6打印(按小时交换平均值)
7.
---->8已排序的交换=已排序(按小时交换平均值,反向=真)
9
10打印(已排序的交换)
TypeError:“列表”对象不可调用

不确定如何上载csv,因为看不到上载选项。代码中有明显的错误吗?或者有人能帮你上传csv文件吗?

你需要先声明这两个列表,然后再应用
avg_by_hour=[]swap_avg_by_hour=[]

谢谢你查看:)我声明了这两个列表,但列表[“list object is not callable”]的类型错误仍然存在。您是否注意到可能存在问题的任何其他方面?请将列表交换平均值按小时添加到问题中