gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:32:10 24 4
gpt4 key购买 nike

我有这个矩阵:

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

关于python - 从元组中查找矩阵中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37427929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com