“值解包过多”但我返回两个列表并分配给两个变量

0 投票
1 回答
976 浏览
提问于 2025-04-20 13:30

我有一个函数,它返回两个列表,像这样:

return foo, bar

当我调用这个函数时,我是这样做的:

food, bar = function(x,y,z)

但是我遇到了一个错误,提示我有太多的值无法拆分。

还有什么其他原因可能导致这个错误呢?

以下是代码:

def beat_counter(timestamp, data, threshold):

    ##Error Checking
    if(len(timestamp) != len(data)):
        print "Timestamp and data must have same size"
        print "Timestamp size ", len(timestamp)
        print "Data size ", len(data)
        return

    if(threshold <= 0 or threshold > 100):
        print "threshold must be greather than zero, please choose reasonable size, 1 => 1 beat BPM estimation"

    print "running..."

    BPMs = [] #store list of bpms
    BPMTimes = []

    crossThreshold = 2 * threshold #number of zero crosses to detect is twice the number of beats

    #TEST STATEMENT
    #print "inside function "
    #print "threshold ", threshold
    #print "cross threshold ", crossThreshold

    for indx, val in enumerate(timestamp): #for all timestamps

        zeroCrossings = 0 #set zero crossings

        i = indx #set looping variable to current indx

        #TEST STATEMENT
        print "Inside iteration, index ", indx

        #while(zeroCrossings < crossThreshold): #while you have not met the threshold
        for i in range(indx, len(data)):

            if(zeroCrossings >= crossThreshold):
                curBPMTime = timestamp[i] #set curBPM to timestamp when the threshold is met
                break

            #TEST STATEMENT
            #print "while loop at index ", i
            #print "previous point ", data[i-1]
            #print "current point ", data[i]

            if(i == indx): #if this is the start of the window only check first condition
                if(data[i] == 0): #if the very first data point lies on zero
                    zeroCrossings += 1 #increase zero crossings
                    continue
                else:
                    continue #otherwise continue because you need two non zero data points to determine zero crossings            


            #check for zero cross (including data points that are at zero)
            if( data[i] == 0): #if this datapoint is zero add to zero cross and move to next data, this counts the indx at zero because j starts at zero
                zeroCrossings += 1
                #TEST STATEMENT
                #print "zero cross case at0, now at", zeroCrossings

            elif( (data[i] > 0) and (data[i-1] < 0) ): # or if the last point was below zero and this one is above zero
                zeroCrossings += 1
                #TEST STATEMENT
                #print "zero cross case neg->pos, now at", zeroCrossings

            elif( (data[i] < 0) and (data[i-1] > 0) ): # or if the last point was above zero and this one is below zero
                zeroCrossings += 1
                #TEST STATEMENT
                #print "zero cross case pos->neg, now at", zeroCrossings

            i += 1 #increment i

            if(i >= len(data)): #reached end of data without hitting threhold
                # Didn't hit threshold number of beats
                return BPMs

            #Test Statment
            print "Zero Crosses so far ", zeroCrossings

        #beats counted, should be equal to threshold unless something odd
        beatsCounted = zeroCrossings/2 #two crosses is one beat

        #zeroCrossings is now equal to crossThreshold, find amount of time that elapsed
        elapsedTime = timestamp[i-1] - timestamp[indx] # i is one greater than index that met threshold, decrement and find elapsed time

        #find frequency of beats
        beatFreq  = beatsCounted/elapsedTime

        #convert beat frequency to bpm
        curBPM  = beatFreq * 60

        #Append to list
        BPMs.append(curBPM)

        #create related timestamp
        BPMTimes.append(curBPMTime)

        print ""
        print "BPMS"
        print BPMs
        print "length: ", len(BPMs)

        print "BPM Times"
        print BPMTimes
        print "length: ", len(BPMTimes)

    return BPMTimes, BPMs

1 个回答

4

在你的函数里,有几个不同的 return 语句:

    return

            return BPMs

return BPMTimes, BPMs

你需要确保你的函数总是返回一个包含两个元素的元组(2-tuple),否则你会遇到你之前看到的错误。

撰写回答