Python上的矩阵和索引

时间:2019-02-14 15:47:33

标签: python function matrix indexing

我应该编写一个函数,该函数接收大小为N x N的整数M以及两个索引“ ir”和“ jc”的矩阵,并且当且仅当针对“ ir”行中的每个元素x返回“ True” ,M在“ ir”行和“ jc”列之外至少包含两个等于x的元素。为什么列表索引超出范围?

def verifica(M, ir, jc):
    elementi_uguali = 0
    for n in M[ir]:
        contatore = 0
        for i in range(len(M)):
            for j in range(len(M[0])):
                if M[i][j] == n:
                    if i != ir:
                        if j != jc:
                            contatore += 1
                        if contatore == 2:
                            elementi_uguali += 1
                        break
        if elementi_uguali == len(M):
            return True

verifica([[8,0,3,-9,3],
          [2,3,6,3,-1],
          [1,2,5,7,-1],
          [6,9,2,5,3],
          [3,2,4,-16]], 1, 2)

0 个答案:

没有答案