scipy TypeError:无法解包不可迭代的int对象

x33g5p2x  于 2023-01-17  发布在  其他
关注(0)|答案(4)|浏览(180)

我如何解决这个错误在运行我的代码如下.我正在使用下面的函数和implementin运行窗口for循环,但最终得到下面的错误. for循环工作,挂在一个点.

def get_grps(s, thresh=-1, Nmin=3):
    """
    Nmin : int > 0
    Min number of consecutive values below threshold.
    """
    m = np.logical_and.reduce([s.shift(-i).le(thresh) for i in range(Nmin)])
    if Nmin > 1:
        m = pd.Series(m, index=s.index).replace({False: np.NaN}).ffill(limit=Nmin - 1).fillna(False)
    else:
        m = pd.Series(m, index=s.index)

    # Form consecutive groups
    gps = m.ne(m.shift(1)).cumsum().where(m)

    # Return None if no groups, else the aggregations
    if gps.isnull().all():
        return 0
    else:
        agg = s.groupby(gps).agg([list, sum, 'size']).reset_index(drop=True)
        # agg2 = s2.groupby(gps).agg([list, sum, 'size']).reset_index(drop=True)
        return agg, gps

data_spi = [-0.32361498 -0.5229471   0.15702732  0.28753752   -0.01069884 -0.8163699
  -1.3169327   0.4413181   0.75815576  1.3858147   0.49990863-0.06357133
-0.78432    -0.95337325 -1.663739    0.18965477  0.81183237   0.8360347
  0.99537593 -0.12197364 -0.31432647 -2.0865853   0.2084263    0.13332903
 -0.05270813 -1.0090573  -1.6578217  -1.2969246  -0.70916456   0.70059913
 -1.2127264  -0.659762   -1.1612778  -2.1216285  -0.8054617    -0.6293912
 -2.2103117  -1.9373081  -2.530625   -2.4089663  -1.950846    -1.6129876]
lon = data_spi.lon
lat = data_spi.lat
print(len(data_spi))

n=6

for x in range(len(lat)):
    for y in range(len(lon)):
        if data_spi[0, x, y] != 0:
            for i in range(len(data_spi)-70):
                ts = data_spi[i:i+10, x, y].fillna(1)
                print(ts)
                # print(np.array(ts))

                agg, gps = get_grps(pd.Series(ts), thresh=-1, Nmin=3)

                duration = np.nanmean(agg['sum'])
                frequency = len(agg['sum'])
                severity = np.abs(np.mean(agg['sum']))
                intensity = np.mean(np.abs(agg['sum'] / agg['size']))
                print(f'intensity {intensity}')

我得到这个错误

Traceback (most recent call last):
 File "/Users/mada0007/PycharmProjects/Research_ass /FREQ_MEAN_INT_DUR_CORR.py", line 80, in <module>
 agg, gps = get_grps(pd.Series(ts), thresh=-1, Nmin=3)
 typeError: cannot unpack non-iterable int object

如何解决此错误?

ujv3wf0j

ujv3wf0j1#

只需将return 0替换为return 0, 0或更好的版本:引发错误而不是返回0
当你的if条件为True时,你只返回0,然后,当你执行agg, gps = get_grps(...)时,你告诉python解包函数的结果,然后,python期待一个2长度的可迭代对象,并试图解包它,但正如它所说:它"无法解包不可迭代的int对象...“
所以一个快速的解决方法是用return 0, 0返回一个元组(0,0),但这是相当糟糕的,因为你在期望对象的地方返回了整数。你的脚本将在下一行duration = np.nanmean(agg['sum'])崩溃(因为agg是0)。
处理这种情况的一些更干净的解决方案是第二次拆包:

def get_grps(s, thresh=-1, Nmin=3):
    # ...
    if gps.isnull().all():
        return None
    else:
        # ...
        return agg, gps

for i in range(len(data_spi)-70):
    ts = data_spi[i:i+10, x, y].fillna(1)

    result = get_grps(pd.Series(ts), thresh=-1, Nmin=3)
    if result is None:
        break

    agg, gps = result

    duration = np.nanmean(agg['sum'])
    frequency = len(agg['sum'])
twh00eeo

twh00eeo2#

类似的错误已经出现,所以为了解决这个问题,我发布了一个例子。希望它会有所帮助。
原因:由于int中没有__itr__方法,我们不能像在listtupledict{}中那样迭代它。

x,y,z,n = input() # NOT CONVERTING THE INPUT TO 'INT' HERE  

l = []  

for a in range(0,int(x)): # converting the input to INT  
    for b in range(0,int(y)): # converting the input to INT  
        for c in range(0,int(z)): # converting the input to INT  
            if a+b+c!= n:  
                l.append([a,b,c])  
print(l)
nnsrf1az

nnsrf1az3#

**(DJANGO)**在我的例子中,我试图获取一个项目,但没有设置搜索参数:

schooling = Schooling.objects.get(randint(1, 10)) #Error

 schooling = Schooling.objects.get(id=randint(1, 10)) #OK set id=
x3naxklr

x3naxklr4#

我在Django中遇到了同样的错误:
TypeError:无法解包不可迭代的int对象
使用get(20)或filter(20)检索id时,如下所示:

# Here
obj = Person.objects.get(20)
# Here
qs = Person.objects.filter(20)

因此,我使用get(id=20)filter(id=20)检索id,如下所示,然后错误得到解决:
一个二个一个一个

相关问题