下面的算法是为了在列表中使用list编写的,但是我确信它可以很容易地调整以使用numpy。我还没用过numpy,所以我不能给你确切的答案。在def main():
grid = [[0, 0, 0, 1],
[0, 0, 1, 1],
[2, 2, 2, 2]]
borders = []
'''
It has the following format.
['Borders beetwen 0 and 1', [[0, 2], [0, 3]]]
[0, 2] is coordinate of 0 and [0, 3] is coordinate of 1.
'''
for x in range(0, len(grid)-1):
for y in range(0, len(grid[x])-1):
if grid[x][y] != grid[x][y+1]:
header = ['Borders beetwen %s and %s' %(str(grid[x][y]), str(grid[x][y+1]))]
coords = [[x, y], [x, y+1]]
header.append(coords)
borders.append(header)
else:
pass
if grid[x][y] != grid[x+1][y]:
header = ['Borders beetwen %s and %s' %(str(grid[x][y]), str(grid[x+1][y]))]
coords = [[x, y], [x+1, y]]
header.append(coords)
borders.append(header)
else:
pass
for element in borders:
print(element)
if __name__ == '__main__':
main()