arrays - removing python reference, copying instead -
so have strange error in function think must have python references rather value assignment. if can see causing value of mainlist disappear grateful. - http://pastebin.com/6szcwak8
inputs: ships -> [0] hitlistlength = 1 output of first print statement [[0]] output of second print statement [[]]
my theory because have popped countinglist
has removed value mainlist
not sure how round this?
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 array use on recusion level ship = ships.pop() #loop through each hit x in range(start,hitlistlength): #add rotation counting list countinglist.append(x) #if don't need go deeper add counting array element of 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 loops countinglist contribution next loop can take place countinglist.pop() #return mainlist ships.append(ship) print "mainlist: " + str(mainlist) return mainlist
you do
mainlist.append(countinglist)
and
countinglist.pop()
which removes element countinglist leaves [] in mainlist
because append appends reference countinglist , pop element countinglist change gets reflected in mainlist well
use copy if want store copy of countinglist in mainlist
Comments
Post a Comment