简体   繁体   中英

Python ValueError: too many values to unpack in a While loop

I am getting this exception from the following code and mainly form the second line in the while loop, any hint please? Thank you.

def SampleLvl(self, mods, inds, M):
    calcM = 0
    total_time = 0
    p = np.arange(1, self.last_itr.computedMoments()+1)
    psums_delta = _empty_obj()
    psums_fine = _empty_obj()
    while calcM < M:
        curM = np.minimum(M-calcM, self.params.maxM)
        values, samples_time = self.fn.SampleLvl(inds=inds, M=curM)
        total_time += samples_time

        delta = np.sum(values * \
                       _expand(mods, 1, values.shape),
                       axis=1)
        A1 = np.tile(delta, (len(p),) + (1,)*len(delta.shape) )
        A2 = np.tile(values[:, 0], (len(p),) + (1,)*len(delta.shape) )
        B = _expand(p, 0, A1.shape)
        psums_delta += np.sum(A1**B, axis=1)
        psums_fine += np.sum(A2**B, axis=1)
        calcM += values.shape[0]

    return calcM, psums_delta, psums_fine, total_time

I got this error

, line 740, in SampleLvl values, samples_time = self.fn.SampleLvl(inds=inds, M=curM) ValueError: too many values to unpack

In this line:

values, samples_time = self.fn.SampleLvl(inds=inds, M=curM)

you assign the result of SampleLvl to 2 variables, but your function, SampleLvl , that you seem to call recursively in that line, returns a 4-tuple. I assume that self.fn.SampleLvl is the same function you're in already. If that's the case you've also omitted the mods param in the call.

Another remark is that a bit more context would be handy. I just assume that there's only one SampleLvl , so self==self.fn , but there may in fact be 2 different functions with the same name involved, which, without context, I find confusing.

You will get a ValueError: too many values to unpack when you try and assign more variables into less variables.

For example, if you had a function foo() which returns (a, b, c) you could do: a, b, c = foo() but you would get an error if you tried to do a, b = foo() as the function returns more variables than you are trying to assign to.

You do this here:

values, samples_time = self.fn.SampleLvl(inds=inds, M=curM)

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM