返回空的函数支持清除临时数据

2024-04-24 09:03:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的脚本,它会让我产生类似的结果:

 5 average
                        --->  No range de 12 ocorrências (taxa) ----> in block 60 occurrences
 5 stantard derivation

例如:

[[[69, 2.0], [69, 2.0], [64, 3.872983346207417], [71, 1.4142135623730951], [80, 4.0]], [[69, 2.0], [69, 2.0], [64, 3.872983346207417], [71, 1.4142135623730951], [80, 4.0]], [[69, 2.0], [69, 2.0], [64, 3.872983346207417], [71, 1.4142135623730951], [80, 4.0]]]

但他把这个还给了我:

[[], [], [], []]

对这个问题有什么建议吗

我不明白为什么在清除了临时列表之后,主列表也被清除了

插入数据:

[68 68 69 68 69 70 71 75 72 73 72 72 73 72 72 73 70 71 73 72 72 71 69 68
69 68 69 68 69 68 68 68 68 69 68 69 70 71 75 72 69 68 68 68 69 68 69 70
71 75 72 69 68 69 68 69 68 69 68 68 60 60 61 60 61 65 69 69 72 73 72 72
73 72 72 73 70 71 73 75 78 80 82 84 87 84 84 83 82 79 78 76 74 73 72 72
72 71 75 72 69 68 68 68 69 68 69 70 71 75 72 69 68 69 68 69 68 69 68 68]

脚本:

def standardDeviation(data):
    return statistics.stdev(data)
       
def mean(data):
    return statistics.mean(data)

def captureOcurrences(elements, n):
    L = len(elements)
    return [elements[i: i+n] for i in range(0, L, n)]

def input(elements):

    result = []
    temp = []
    start = 0
    limit = 60
    size = int(len(elements))
    TargetDivision = int(size / 30)
    repetitions = 0
    five = 0

    while repetitions < TargetDivision:

        five += 1
        ocurrences = captureOcurrences(elements[start: limit],12)
        for i in ocurrences:
            print("[INFO] 12 Ocurrences: {}".format(i))
            print("[INFO] Mean: {}".format(mean(i)))
            m = mean(i)
            print("[INFO] Standard Deviation: {}".format(standardDeviation(i)))
            sd = standardDeviation(i)
            print("Result: [{},{}]\n\n".format(m,sd))
            temp.append([m,sd])

        print("[INFO] Result Cycle {}: \n{}\n\n".format(repetitions+1,result))
        result.append(temp)
        temp.clear()
        repetitions += 1
        limit += 10
        start += 10

    return result

Y = data.iloc[:, 1].values
print(input(Y))

1条回答
网友
1楼 · 发布于 2024-04-24 09:03:28

您没有正确处理“temp”变量:

    while repetitions < TargetDivision:
        temp = []

        five += 1
        ocurrences = captureOcurrences(elements[start: limit],12)
        for i in ocurrences:
            print("[INFO] 12 Ocurrences: {}".format(i))
            print("[INFO] Mean: {}".format(mean(i)))
            m = mean(i)
            print("[INFO] Standard Deviation: {}".format(standardDeviation(i)))
            sd = standardDeviation(i)
            print("Result: [{},{}]\n\n".format(m,sd))
            temp.append([m,sd])

        print("[INFO] Result Cycle {}: \n{}\n\n".format(repetitions+1,result))
        result.append(temp)
        # Don’t clear temp.

相关问题 更多 >