Skip to main content
 首页 » 编程设计

python之从元组中查找矩阵中的值

2025年05月04日8kerrycode

我有这个矩阵:

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 中不 <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) 

请您参考如下方法:

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)]