Python列表不复制

2024-10-01 15:41:48 发布

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

在下面的子集问题中,我试图复制一个list对象

def findFourPlus(itemCount, seq, goal):
    goalDifference = float("inf")
    closestPartial = []
    subset_sum(itemCount, seq, goal, goalDifference, closestPartial, partial=[])
    print(closestPartial)


def subset_sum(itemCount, seq, goal, goalDifference, closestPartial, partial):
    s = sum(partial)

    # check if the partial sum is equals to target
    if(len(partial) == itemCount):
        if s == goal:
            print(partial)
    else:
        if( abs(goal - s) < goalDifference):
            goalDifference = abs(goal - s)
            print(goalDifference)
            print(partial)
            print(closestPartial)
            closestPartial = copy.deepcopy(partial)        

for i in range(len(seq)):
    n = seq[i]
    remaining = seq[i+1:]
    subset_sum(itemCount, remaining, goal, goalDifference, closestPartial, partial + [n])

在subset函数中,我试图将partial列表复制到closestPartial。我试过了

closestPartial = partial
closestPartial = list[:]
closestPartial = list(partial)
closestPartial = copy.copy(partial)
closestPartial = copy.deepcopy(partial)

但最终所有这些似乎都是徒劳的。由于某些原因,closestPartial仍然是一个空列表(这就是我最初想要的)


Tags: lenifdefabspartialseqlistsum
2条回答

您将closestPartial作为参数传入,因此唯一有效的方法是对其列表进行就地更新。您给出的所有示例都将closestPartial中的列表替换为新列表。但因为不是你传来的名单,所以不会更新真正的名单。你知道吗

尝试:

closestPartial[:] = partial

您可以通过在操作前后打印列表id来了解问题。你知道吗

print id(closestPartial)
...some operation
print id(closestPartial)

如果id发生了变化,则表示您创建了一个新列表,而没有更新传入的列表。你知道吗

编辑

看来我需要一个更好的解释。。。调用subset_sum时,它会创建一个名为closestPartial的局部变量,该变量引用作为参数传入的任何内容,在本例中,调用方称之为closestPartial的列表。现在有两个变量指向同一个列表。如果重新分配变量,如closestPartial = partial,那么这两个变量现在指向不同的列表。您没有更新调用者的指针,只是更改了局部变量。相反,如果不重新分配,调用方也会看到对两个变量引用的一个列表所做的更改,因为它是同一个列表。你知道吗

我怀疑您的goalDifference也有同样的问题,如果您在函数中更改它,然后期望更改后的值以某种方式返回调用函数。你知道吗

下面是一些(Python 2风格)代码来说明发生了什么:

#! /usr/bin/env python

def testA(update_func):
    seq = []
    num = 1
    for _ in range(5):
        newnum = update_func(seq, num)
        print 'testA:  ', num, seq, newnum
    print

def testB(update_func):
    seq = []
    num = 1
    for _ in range(5):
        num = update_func(seq, num)
        print 'testB:  ', num, seq
    print


def update0(seq, num):
    #This creates a new list
    seq = seq + [num]
    num = num + 1
    print 'update0:', num, seq
    return num

def update1(seq, num):
    #This updates the existing list
    seq.append(num)
    num += 1
    print 'update1:', num, seq
    return num

def update2(seq, num):
    #This updates the existing list
    seq[:] = seq + [num]
    num += 1
    print 'update2:', num, seq
    return num

def update3(seq, num):
    #This updates the existing list
    seq += [num]
    num += 1
    print 'update2:', num, seq
    return num


update_funcs = (update0, update1, update2, update3)
for f in update_funcs:
    testA(f)

print '   \n'

for f in update_funcs:
    testB(f)

堆栈溢出成员nedbatchelder的文章Facts and myths about Python names and values有很好的解释,有可爱的图表。你知道吗

相关问题 更多 >

    热门问题