TypeError:'int'对象不可迭代-使用POOL进行多重处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError:'int'对象不可迭代-使用POOL进行多重处理相关的知识,希望对你有一定的参考价值。

我正在尝试读取csv文件并查找列的平均值。无法破解池多处理部分!继续为多重处理部分提供错误。串行处理正常!

import os
from multiprocessing import Pool
import pandas as pd
import time
import statistics
    def average(listed_marks):
        avg=[]
        i=[]
        for i in listed_marks:
            avg.append(statistics.mean(i))

        return avg



    if __name__ == '__main__':

        list_of_marks=[]  

        df = pd.read_csv(r"C:UsersRadhikaDesktopRADZ ProjectsXoriantdata.csv")
        n = df['STUDENT ID'].count() #no of rows

        for i in range(n):  #to obtain the list of lists
            a=list(df.iloc[i , 3:6])
            a = list(map(int, a)) #converting the lists into int to perform mean
            list_of_marks.append(list(a))

    # ---------------------MP---------------------------------------------------

        s1= time.time()    

        p = Pool()
        avg_mp = p.map(average, list_of_marks) #passed the average function and iterator is a list of list
        df['Average'] = avg_mp
        p.close()
        p.join()

        print(f"Processing took {time.time() - s1} using Multi-Processing")
答案

看起来您的average()函数应该只是:

def average(listed_marks):
   return statistics.mean(listed_marks)

以上是关于TypeError:'int'对象不可迭代-使用POOL进行多重处理的主要内容,如果未能解决你的问题,请参考以下文章