python checking if coordinates already exist in list -
this work assignment. working 2d array , far have found path want take through array. there other paths in array , want able go through them cannot reuse paths. array looks like:
0,2,4,1,6,0,0 2,0,0,0,5,0,0 4,0,0,0,5,5,0 1,0,0,0,1,1,0 6,5,0,1,0,5,5 0,0,5,1,5,0,0 0,0,0,0,5,0,0
my code finding nearby neighbours is:
def check_neighbours(alist, node): nodes = [] in range(0,2): j in range(0,2): x = node[0]+i y = node[1]+j if x>=0 , y>=0 , (i!=0 or j!=0) , alist[x][y]>0: nodes.append([x, y])
i appending each visited coordinate x,y list builds full path taken. here example of output:
inside pathing function path taken ['[0, 1]', '[0, 2]', '[0, 3]', '[0, 4]', '[1, 4]', '[2, 4]', '[2, 5]', '[3, 5]', '[4, 5]', '[4, 6]']
because of way searching neighbours (having x , y separated) cannot think of way of testing whether or not current coordinate built located inside of list. think code fit in between following 2 lines:
if x>=0 , y>=0 , (i!=0 or j!=0) , alist[x][y]>0: nodes.append([x, y])
what about
nodes = [] nodes.append([1,1]) nodes.append([2,2]) nodes.append([3,3]) [1,1] in nodes # true [1,3] in nodes # false
i'm not clear on question, may off.
that being said, since, in order added nodes
list, has either 1 column right, 1 below, or both, of previous node. , since you're considering nodes right , below (range(0,2)
), can never duplicates in list.
Comments
Post a Comment