Python线程错误 – 必须是可迭代的,而不是int

我正在尝试计算数据帧中第一列和其他列之间的回归的滚动r平方(第一列和第二列,第一列和第三列等)但是当我尝试线程时,它一直告诉我错误

TypeError: ParallelRegression() argument after * must be an iterable, not int”.

我想知道如何解决这个问题?非常感谢!

import threading

totalThreads=3 #three different colors
def ParallelRegression(threadnum):
    for i in range(threadnum):
        res[:,i]=sm.OLS(df.iloc[:,0], df.iloc[:,i+1]).fit().rsquared
threads=[]
for threadnum in range(totalThreads):
    t=threading.Thread(target=ParallelRegression,args=(threadnum))
    threads.append(t)
    t.start()
for threadnum in range(totalThreads):
    threads[threadnum].join()

查看下面链接图片中的数据摘要(df):

最佳答案 threading.Thread类需要一个可迭代的参数作为args参数.你传递args =(threadnum)是一个单个int对象,你需要传递一些允许多个args的可迭代对象,即使你只想传递一个arg.

args = [threadnum]会起作用,因为这会产生一个可迭代的列表.

点赞