2014-09-10 2 views
-1

그래서이 함수에는 값 할당보다는 파이썬 참조와 관련이 있어야한다고 생각하는 이상한 오류가 있습니다. 누군가 mainList의 가치를 사라지게하는 것을 볼 수 있다면 나는 매우 감사 할 것입니다. - http://pastebin.com/6sZCwAk8대신 파이썬 참조를 제거합니다.

inputs: 
ships -> [0] 
hitListLength = 1 
output of first print statement [[0]] 
output of second print statement [[]] 

내 이론은 내가 countingList에서 튀어 때문에 그것은이 라운드하는 방법을하지 않도록 mainList에서 값을 제거했다고입니까?

def addPlacement(ships,hitListLength,start = None, mainList = None,countingList = None): 
    if start == None: 
     start=0 
    if mainList == None: 
     mainList = [] 
    if countingList == None: 
     countingList = [] 
    #pop ship from array for use on this recusion level 
    ship = ships.pop() 
    #loop through each hit 
    for x in range(start,hitListLength): 
     #add this rotation to the counting list 
     countingList.append(x) 
     #if we don't need to go any deeper add the counting array as an element of the main list 
     if len(ships) == 0: 
      mainList.append(countingList) 
      print "MainList: " + str(mainList) 
     else: 
      #otherwise recure deeper updating mainlist 
      mainList += addPlacement(ships,hitListLength,(start+1),mainList,countinglist) 
     #remove this loops countingList contribution so next loop can take its place 
     countingList.pop() 
    #return the mainlist 
    ships.append(ship) 
    print "MainList: " + str(mainList) 
    return mainList 
+0

당신이 딥 카피 인 할 방법을 countingList의 사본을 저장하려면 mainList에서뿐만 아니라

사용 복사'된 newval = copy.deepcopy은 (인 oldval)',하지만 난 그렇게 확실하지 않다 그게 여기서 문제입니다. – cdarke

+0

mainlist.append (countingList)를 mainlist.append (countingList [:])로 변경합니다. countingList를 분할하면 빈 mainList를 반환하는 문제를 해결해야합니다. – Ram

답변

0

당신은

mainList.append(countingList) 

와 [] 당신을 잎 countingList에서 유일한 요소를 제거 후

countingList.pop() 

APPEND가에 대한 참조를 추가하기 때문에 mainList

에서 countingList 및이 ​​변경 사항을 반영하는 countingList 유일한 요소를 팝합니다. 당신이 mainList

관련 문제