简体   繁体   中英

Python: appending array index to list (.append)

Thanks ahead of time for the help. I am having trouble with appending an array index to a list. I Have an array counterStack which contains the starting point (1366, 1264) I run a neighborhood search on that starting point and for every new index that satisfies the conditions set the index should be appended to the counterStack. The strange thing is indices get properly appended to the list stack , but not list counterStack .

Is this because of my if statement ( if ([nx, ny] not in counterStack): )? I know it enters the if statement because the counter count gets plus one through each iteration. Indices will get added to stack twice. So I set up the counterStack to only account for an index once so I can limit the overall edited pixels. By the end of the run there should be 121 indeces appended to counterStack.

This is an example of the results:

count =  1
[(1366, 1264), (1365, 1263)]
count =  2
[(1366, 1264), (1365, 1264)]
count =  3
[(1366, 1264), (1365, 1265)]
count =  4
[(1366, 1264), (1366, 1265)]
count =  5
[(1366, 1264), (1367, 1265)]
count =  6
[(1366, 1264), (1367, 1264)]
count =  7
[(1366, 1264), (1367, 1263)]
count =  8
[(1366, 1264), (1366, 1263)]
count =  9
[(1366, 1264), (1365, 1262)]

Here is some of my code:

neighbors = [(-1,-1), (-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1)]
mask = np.zeros_like(dem_arr, dtype = bool)
stack = [(1366, 1264)] # push start coordinate on stack

if (dem_arr[1366, 1264] > -100):
    count = 0
    while count <= 121:
        x, y = stack.pop()
        mask[x, y] = True
        for dx, dy in neighbors:
            nx, ny = x + dx, y + dy
            if (0 <= nx < dem_arr.shape[0] and 0 <= ny < dem_arr.shape[1] and dem_arr[x, y] > -100 and dem_arr[nx, ny] > -100 and not mask[nx, ny] and abs(dem_arr[nx, ny] - dem_arr[x, y]) <= 5):    #set elevation differnce
                stack.append((nx, ny))  #if point is selected (true) array position gets added to stack and process runs over again
                counterStack = [(1366, 1264)]
                if ([nx, ny] not in counterStack):
                    counterStack.append((nx, ny)) 
                    dem_copy[(nx, ny)] = 8888
                    #dem_copy[randx, randy] = 8888
                    count += 1
                    print 'count = ', count
                    print counterStack

else:
    print 'Point chosen has no data'
    randx = random.randint(0, row-1)
    randy = random.randint(0, col-1)

Thanks again for the help!

-R

You're setting counterStack = [(1366, 1264)] in each iteration of the for loop but stack is only set to the base index once.

Move counterStack = [(1366, 1264)] to right below the line stack = [(1366, 1264)] and you should see what you want.

Also as 9000 noted, (x, y) indicates a 2-tuple consisting of x and y . [x, y] is a list of length 2. You can compare results individually with index values: (x, y)[1] will return y and [x, y][1] will return y as well.

But using if ... not in counterStack will compare each 2-tuple (xx, yy) in counterStack to a list [nx, ny] and it's never going to return False because counterStack will never have a list .

Here's your problem:

        counterStack = [(1366, 1264)] # counterStack contains a tuple 
        if ([nx, ny] not in counterStack):  # counterStack is checked for a list
            counterStack.append((nx, ny))   # counterStack adds another tuple

Note that [1, 2] and (1, 2) are completely different things. They are not equal to each other, so in does not do what you'd expect.

I assume you use 2-tuples uniformly to denote a position. Tuples are immutable and fixed size, which makes total sense for a coordinate vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM