首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python列表不复制

python列表不复制
EN

Stack Overflow用户
提问于 2014-09-22 02:48:00
回答 2查看 69关注 0票数 0

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

代码语言:javascript
运行
复制
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])

在子集函数中,我试图将partial列表复制到closestPartial.我试过了

代码语言:javascript
运行
复制
closestPartial = partial
closestPartial = list[:]
closestPartial = list(partial)
closestPartial = copy.copy(partial)
closestPartial = copy.deepcopy(partial)

但最终看来,所有这些都是徒劳的。由于某些原因,closestPartial仍然是一个空列表(这是我发起的)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-22 02:56:39

您将closestPartial作为参数传入,因此唯一起作用的是对其列表进行内部更新。给出的所有示例都将closestPartial中的列表替换为新列表。但是,因为它不是您传入的列表,所以它不会更新真正的列表。

尝试:

代码语言:javascript
运行
复制
closestPartial[:] = partial

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

代码语言:javascript
运行
复制
print id(closestPartial)
...some operation
print id(closestPartial)

如果id更改,这意味着您创建了一个新列表,而没有更新传入的列表。

编辑

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

票数 1
EN

Stack Overflow用户

发布于 2014-09-22 06:48:59

我怀疑您的goalDifference正遭受同样的问题,如果您在一个函数中更改它,然后期望更改的值以某种方式返回到调用函数。

下面是一些(Python 2风格的)代码来说明正在发生的事情:

代码语言:javascript
运行
复制
#! /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)

堆栈溢出成员奈德巴奇尔德的文章关于Python名称和值的事实和神话有一个很好的解释,用可爱的图表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25966044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档