我有这个矩阵:
matrix = np.array([[3,3,3,3,3,3,3,3,3,3,3,3],
[3,2,2,2,2,2,0,0,0,0,0,3],
[3,2,2,2,2,0,0,0,0,0,0,3],
[3,2,2,2,0,0,0,0,0,0,0,3],
[3,2,2,0,0,0,0,0,0,0,0,3],
[3,2,0,0,0,0,0,0,0,0,0,3],
[3,0,0,0,0,0,0,0,0,0,1,3],
[3,0,0,0,0,0,0,0,0,1,1,3],
[3,0,0,0,0,0,0,0,1,1,1,3],
[3,0,0,0,0,0,0,1,1,1,1,3],
[3,0,0,0,0,0,1,1,1,1,1,3],
[3,3,3,3,3,3,3,3,3,3,3,3]])
以及列有这些元组的列表列表:
[[(10, 6), (10, 5), (10, 7), (9, 6), (9, 5), (9, 7)], [(9, 7), (9, 6), (9, 8), (8, 7), (8, 6), (8, 8), (10, 7), (10, 6), (10, 8)], [(10, 7), (10, 6), (10, 8), (9, 7), (9, 6), (9, 8)], [(8, 8), (8, 7), (8, 9), (7, 8), (7, 7), (7, 9), (9, 8), (9, 7), (9, 9)], [(9, 8), (9, 7), (9, 9), (8, 8), (8, 7), (8, 9), (10, 8), (10, 7), (10, 9)], [(10, 8), (10, 7), (10, 9), (9, 8), (9, 7), (9, 9)], [(7, 9), (7, 8), (7, 10), (6, 9), (6, 8), (6, 10), (8, 9), (8, 8), (8, 10)], [(8, 9), (8, 8), (8, 10), (7, 9), (7, 8), (7, 10), (9, 9), (9, 8), (9, 10)], [(9, 9), (9, 8), (9, 10), (8, 9), (8, 8), (8, 10), (10, 9), (10, 8), (10, 10)], [(10, 9), (10, 8), (10, 10), (9, 9), (9, 8), (9, 10)], [(6, 10), (6, 9), (5, 10), (5, 9), (7, 10), (7, 9)], [(7, 10), (7, 9), (6, 10), (6, 9), (8, 10), (8, 9)], [(8, 10), (8, 9), (7, 10), (7, 9), (9, 10), (9, 9)], [(9, 10), (9, 9), (8, 10), (8, 9), (10, 10), (10, 9)], [(10, 10), (10, 9), (9, 10), (9, 9)]]
这是我要查找的数字及其相邻的空格,在y或x中不是< t< 10,并且我使用函数在填充0的空格之间对它们进行分类或者其他任何东西,并将它们附加到列表中。如何更正进行分类的功能?
def classify(neighbors,matrix):
for x in neighbors:
y = x[0]
z = x[1]
if matrix[y][z] == 0:
step.append(x)
else:
hop.append(x)
print(hop,step)
答案 0 :(得分:1)
import numpy as np
from array import *
matrix = np.array([[3,3,3,3,3,3,3,3,3,3,3,3],
[3,2,2,2,2,2,0,0,0,0,0,3],
[3,2,2,2,2,0,0,0,0,0,0,3],
[3,2,2,2,0,0,0,0,0,0,0,3],
[3,2,2,0,0,0,0,0,0,0,0,3],
[3,2,0,0,0,0,0,0,0,0,0,3],
[3,0,0,0,0,0,0,0,0,0,1,3],
[3,0,0,0,0,0,0,0,0,1,1,3],
[3,0,0,0,0,0,0,0,1,1,1,3],
[3,11,11,0,0,0,0,1,1,1,1,3],
[3,12,12,0,0,0,1,1,1,1,1,3],
[3,3,13,3,3,3,3,3,3,3,3,3]])
neighbors = [[(10, 6), (10, 5), (10, 7), (9, 6), (9, 5), (9, 7)],(10, 5),[(9, 7), (9, 6), (9, 8), (8, 7), (8, 6), (8, 8), (10, 7), (10, 6), (10, 8)]]
def classify(neighbors,matrix):
hop = []
step = []
for x in neighbors:
print x
if isinstance(x,list):
for x1 in x:
y = x1[0]
z = x1[1]
if matrix[y][z] == 0:
step.append(x1)
else:
hop.append(x1)
else:
y = x[0]
z = x[1]
if matrix[y][z] == 0:
step.append(x)
else:
hop.append(x)
print "neighbors which are not having value as zero =" ,hop
print "neighbors which are having zero value =" ,step
classify(neighbors,matrix)
输出: -
neighbors which are not having value as zero = [(10, 6), (10, 7), (9, 7), (9, 7), (9, 8), (8, 8), (10, 7), (10, 6), (10, 8)]
neighbors which are having zero value = [(10, 5), (9, 6), (9, 5), (10, 5), (9, 6), (8, 7), (8, 6)]