English 中文(简体)
类型:无包装的不可阻塞
原标题:TypeError: cannot unpack non-iterable int objec

How can I solve this error After running my code as follows . I am using the function below and implementin running window for loop on it but end up getting the error below. The for loop works and hungs at a point.

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

我如何解决这一错误?

最佳回答

仅替换return 0 by return 0, 0,或更好:产生错误,而不是退回0

When your if condition is True, you only return 0. Then later, when you do agg, gps = get_grps(...), you tell python to unpack the result of the function. Then, python is expecting a 2-length iterable, and try to unpack it, but as it says: it cannot unpack non-iterable int object ...

因此,一个快速的工作是将一个图形(0,0)与<代码>return 0, 0交还,但是,由于你在预计物体的情况下重新分类,这项工作非常糟糕。 页: 1

Some cleaner solutions to handle this case would be to unpack in a second time:

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 ])
问题回答

似乎也出现了类似的错误,以便解决我所说的一个问题,希望这将有助于消除这一错误。

Reason: since int does not have __itr__ method inside it, we can t iterate it over like we do in list or tuple or dict{}.

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)

(DJANGO) In my case, I was trying get a item without set which parameter for search:

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


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

我在Django的错误如下:

类型:不能不包装不可转让的物体

https://docs.djangoproject.com/en/4.1/ref/models/querysets/#django.db.models.query.QuerySet.get”rel=“nofollow noreferer”>(20)或

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

So, I retrieved id with get(id=20) or filter(id=20) as shown below, then the error was solved:

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

在我的Django项目进行测试后,我遇到了类似的错误。

TypeError: cannot unpack non-iterable int object

I discovered a typo error in the creation of the deleteCourse function within my views.py file. The mistake was using a hyphen instead of an equal sign.

def deleteCourse(rqeuest,id):
    course = Course.objects.get(id-id)
    course.delete()
    return redirect( / )


def deleteCourse(rqeuest,id):
    course = Course.objects.get(id=id)
    course.delete()
    return redirect( / )

注意细节是编码的关键。 借此机会彻底审查你的法典,确保准确性和正确性。 它是对意外错误的最佳辩护,确保你的法典顺利运作。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签