2014-09-22 67 views
0

在下面的子問題,我試圖做一個列表對象蟒蛇列表不會複製

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。我試過

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

但是最後他們都看起來是徒勞的。由於某種原因,最接近的部分仍然是一個空列表(這是我發起的)

回答

1

您正在傳遞nearestPartial作爲參數,所以唯一可行的是它的列表的就地更新。您提供的所有示例都會使用新列表替換位於closestPartial中的列表。但由於它不是您通過的列表,它不會更新真實列表。

嘗試:

closestPartial[:] = partial 

您可以通過之前打印列表的ID,並在手術後得到問題的感覺。

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

如果ID發生變化,這意味着你創建一個新的列表,並沒有更新的一個傳入。

編輯

看來我需要一個更好的解釋...當您調用subset_sum,它會創建一個名爲closestPartial的本地變量,該變量引用傳入的任何參數,在這種情況下,調用者已知的列表爲closestPartial。你現在有兩個變量指向同一個列表。如果您重新分配變量,如closestPartial = partial,則這兩個變量現在指向不同的列表。你沒有更新調用者的指針,你只是改變了局部變量。相反,如果你不重新分配,你對兩個變量引用的一個列表所做的更改也會被調用者看到 - 因爲它的列表相同。

+0

不是這樣做的: closestPartial = partial? – 2014-09-22 02:59:04

+0

嗨@Yousef_Shamshoum - 看我的更新。 'nearestPartial = partial'將局部變量賦給名爲closestPartial的局部變量。你想要更新的東西 - 也就是說,在分配之前最接近的部分 - 丟失了。 – tdelaney 2014-09-22 03:01:16

+0

...'[:]'符號在原地進行替換。 – tdelaney 2014-09-22 03:02:03

0

我懷疑你的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) 

堆棧溢出成員內德爾德的文章Facts and myths about Python names and values有一個很好的解釋,與可愛的圖表。

+0

感謝您的鏈接!真正有用的信息,肯定會在未來看看它。至於這個問題,我還是有點失落,所以我覺得自從最初的問題解決以後,我會完全提出一個新的問題 – user3277633 2014-09-22 15:06:40