Python 将压缩迭代器分离为单个迭代器?

Python 将压缩迭代器分离为单个迭代器?,python,for-loop,numpy,iterator,numba,Python,For Loop,Numpy,Iterator,Numba,我的代码有两个2D numpy数组,z和weights。 我像这样迭代它们(在转置它们时): 这很好,直到我开始使用Numba来加速我的代码。使用Numba时,我会出现以下错误: numba.error.NumbaError: (see below) --------------------- Numba Encountered Errors or Warnings --------------------- for y1, w in zip(z.T, weights.T): #

我的代码有两个2D numpy数组,
z
weights
。 我像这样迭代它们(在转置它们时):

这很好,直到我开始使用Numba来加速我的代码。使用Numba时,我会出现以下错误:

numba.error.NumbaError: (see below)
--------------------- Numba Encountered Errors or Warnings ---------------------
        for y1, w in zip(z.T, weights.T): # building the parameters per j class
------------^
Error 82:12: Only a single target iteration variable is supported at the moment
--------------------------------------------------------------------------------
为了解决这个问题,我想我可以这样做:

for y1 in z.T:
   for w in weights.T:
       temp_g = sm.WLS(y1, iself.X, w).fit()

但是我还不太擅长python,所以我只想知道这是否是最好的方法?或者是否有其他更优化的方法?

看起来Numba不支持任务解包。分配给一个目标,然后寻址元组中的两个索引:

for y1_w in zip(z.T, weights.T):
    temp_g = sm.WLS(y1_w[0], iself.X, y1_w[1]).fit()
这里的
y1_w
是一个元组,包含
z.T
weights.T
的成对元素,因此是两个元素的元组。您可以使用索引处理每个元素

您可能可以在循环体中的
for
语句外部使用赋值解包:

for y1_w in zip(z.T, weights.T):
    y1, w = y1_w  # unpack the zip pair 'manually'
    temp_g = sm.WLS(y1, iself.X, w).fit()
for y1_w in zip(z.T, weights.T):
    y1, w = y1_w  # unpack the zip pair 'manually'
    temp_g = sm.WLS(y1, iself.X, w).fit()