Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reinitializing list
#1
Hi Guys,

I have written a function in which I have created a local list of pair values.
In the end, I append it to a global list.

The local list is declared inside for loop.
So when, the control goes inside loop second time or third time, it is not able to reinitialize the list.
Rather it still shows the data which I inserted 1st time.

The function code is attached below.
I am also sharing output.

def shortest(maxHeight, currentRow, currHeight, allPaths) :
   print("\n\nROW : "+str(currentRow))

   tempAllPaths = []

   if(currHeight == 0):
      for i in range (len(currentRow)) :
         if(currentRow[i]==1):
            weight = abs((i-in_pos))+1
            alive  = 1
            tempP = in_pos,currHeight
            pos = in_pos
            list_of_coordinates = []
            list_of_coordinates.append(tempP)
            inc = 1
            if(i<in_pos) :
               inc = -1
            for j in range (in_pos,i,inc):
               tempP = j-1,currHeight
               pos = j-1
               list_of_coordinates.append(tempP)
            print(i)
            print(list_of_coordinates)
            tempAllPaths.append((list_of_coordinates,weight,alive,pos))
      currHeight+=1

   elif(currHeight>0 and currHeight<maxHeight-1):
      print("Number of Paths : "+str(len(allPaths)))
      for a in range(len(allPaths)):
         if(allPaths[a][2] == 1):

            print("Path found alive")
            for i in range (len(currentRow)) :
               if(currentRow[i]==1):
                  tempPath = []
                  tempPath = allPaths[a][0]
                  print(i)

                  path = []
                  path = tempPath

                  print("Current : "+str(tempPath))
                  #print (tempPath)

                  curr_pos = allPaths[a][3]
                  weight = abs(i-curr_pos)+1
                  alive = 1
                  pos  = curr_pos

                  list_of_coordinates = []
                  tempP = curr_pos,currHeight
                  path.append(tempP)


                  inc = 1
                  if(i<curr_pos) :
                     inc = -1
                  for j in range (curr_pos,i,inc):
                     tempP = j-1,currHeight
                     path.append(tempP)
                     pos  = j-1


                  print("Final : "+str(path))
                  tempAllPaths.append((path,weight,alive,pos))
      
      currHeight+=1

   allPaths = tempAllPaths

   if(currHeight==maxHeight-1):
      return 1

   currentRow = amap[(maxHeight-currHeight)-1]
   shortest(maxHeight, currentRow, currHeight, allPaths)
Note : #debug-1 ("Current : "+str(tempPath)) in second run of for loop and #debug-2 (("Final : "+str(path))) in 1st run of for loop
gives same output.

Look at the output below :

Output:
ROW : [0, 0, 1, 0, 0, 0] 2 [(3, 0), (2, 0)] ROW : [1, 1, 0, 1, 0, 0] Number of Paths : 1 Path found alive 0 Current : [(3, 0), (2, 0)] Final : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1)] 1 Current : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1)] Final : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1), (2, 1), (1, 1)] 3 Current : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1), (2, 1), (1, 1)] Final : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1), (2, 1), (1, 1), (2, 1), (1, 1)]
Could some one help me fix this ?
Reply


Messages In This Thread
Reinitializing list - by Shivesh - Jul-08-2019, 02:42 PM
RE: Reinitializing list - by ichabod801 - Jul-08-2019, 02:50 PM
RE: Reinitializing list - by Shivesh - Jul-08-2019, 05:30 PM
RE: Reinitializing list - by ichabod801 - Jul-08-2019, 05:36 PM
RE: Reinitializing list - by Shivesh - Jul-08-2019, 05:53 PM
RE: Reinitializing list - by ichabod801 - Jul-08-2019, 06:07 PM
RE: Reinitializing list - by Shivesh - Jul-08-2019, 09:42 PM
RE: Reinitializing list - by ichabod801 - Jul-08-2019, 09:50 PM
RE: Reinitializing list - by Shivesh - Jul-08-2019, 10:07 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020