首页 > 解决方案 > 使用 .append() 尝试添加新条目时,代码会不断覆盖数组中的条目

问题描述

我最初问了这个问题,询问如何解决我在使用不断增加的变量(store出现在代码末尾的变量)时遇到的问题。实现附加功能可以使变量的大小增加,但由于某种原因,程序并没有按照我的意愿添加新元素,而是在这样做的同时还store用相同的元素覆盖了任何现有条目。该问题的回答者建议我发布一个带有完整代码的新问题,以便可以重现错误。

编码:

from math import floor


def function(x):
    y = sorted(x)  # python sorts this ascending using the sorted function
    y2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    for i in range(len(y)):
        y2[i] = y[len(y) - i - 1]  # this reverses the order of elements in the vector, so it is descending
    m = sum(x) / 11
    a = 0
    for i in range(11):
        if y2[i] >= m:
            a = a + 1
    d = 0
    for i in range(a):
        dummy = y2[i] - m
        d = d + dummy
    rating = round(11 * m + d) / 11
    return floor(rating)


def combinations_with_replacement(iterable, r):
    # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
    pool = tuple(iterable)
    n = len(pool)
    if not n and r:
        return
    indices = [0] * r
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * (r - i)
        yield tuple(pool[i] for i in indices)


def testfn(y, aim):
    leny = len(y)
    nlen = 11 - leny
    choiceset = [81, 82, 83, 84, 85]
    combs = combinations_with_replacement(choiceset, nlen)
    counter = 0
    for i in combs:
        counter = counter + 1
    noofcombs = counter
    z = [[0 for col in range(nlen)] for row in range(noofcombs)]
    combs = combinations_with_replacement(choiceset, nlen) # not sure why i need to include this again, but z doesn't
    # get defined properly without this line
    counter = 0
    for i in combs:
        z[counter] = list(i)
        counter = counter + 1
    store = []
    x0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    for j in range(noofcombs):
        for i1 in range(leny):
            x0[i1] = y[i1] # fills in the rest of the elements of x0 using one of the rows of z
        for i2 in range(nlen):
            x0[i2 + leny] = z[j][i2] # outputs a weighted average of the elements of the vector x0
        var = function(x0)
        if var >= aim - 0.2:
            print(x0) # this is where the error is
            store.append(x0)
            print(store) # this is where the error is


testfn([83, 83, 83, 83, 83, 83, 83, 82], 83)

问题:

我的问题是代码接近尾声(第 55 行及以后)。我认为前两个函数没有任何问题,我只包含了它们,因此代码是可重现的(尽管可能存在错误)。我在第 65、67 行放置了一条打印语句,以便正确显示错误。函数返回的前几件事是:

[83, 83, 83, 83, 83, 83, 83, 82, 81, 81, 84]
[[83, 83, 83, 83, 83, 83, 83, 82, 81, 81, 84]]
[83, 83, 83, 83, 83, 83, 83, 82, 81, 81, 85]
[[83, 83, 83, 83, 83, 83, 83, 82, 81, 81, 85], [83, 83, 83, 83, 83, 83, 83, 82, 81, 81, 85]]

第一个条目显然已被覆盖,我希望第 4 行可以读取

[[83, 83, 83, 83, 83, 83, 83, 82, 81, 81, 84], [83, 83, 83, 83, 83, 83, 83, 82, 81, 81, 85]]

查看进一步的返回显示覆盖仍在继续,我最终留下了一个包含大量副本的数组[83, 83, 83, 83, 83, 83, 83, 82, 85, 85, 85],这不是我想要的。


顺便说一句,如果您想知道,我一直用 y 的元素覆盖 x0 的前几个元素的原因是因为代码的某些部分似乎导致向量 x0 变得有序(降序),所以覆盖确保这些元素始终存在。

标签: python

解决方案


推荐阅读