检查python中的上三角矩阵元素

时间:2016-10-07 16:09:32

标签: python matrix

我有一个上三角矩阵,有很多空元素,但是,我想检查哪些索引有空元素,但我想只检查那些上面的那些,因为下面的那些是空的,所以我不喜欢我想检查那些。所以我想查看图片中显示的元素是否为空,无论n的大小如何,都要覆盖所有情况:enter image description here

这是我的试用版,但它并没有涵盖所有内容,它也在重复,我做错了什么?

for i in range(len(m)):
    for j in range(len(m)):
  words_length = len(words)

                if (m[i][j] == '' and i == j) or (m[i][j] == '' and i== j-3):

                           print ("["),
                           print (i+1),
                           print (","),
                           print (j+1),
                           print ("]"),
                           print(":"),
                           print("-")
                for s in range(0,words_length-1):

                          if ((m[i][j] == '' and i== s and  j == i+1 )  or (m[i][j] == '' and i== 0  and  j== words_length-1)):

                           print ("["),
                           print (i+1),
                           print (","),
                           print (j+1),
                           print ("]"),
                           print(":"),
                           print("-")

                for r in range(0,words_length-3):

                          if (m[i][j] == '' and i==r and  j== i+2 ):
                           print ("chart"),
                           print ("["),
                           print (i+1),
                           print (","),
                           print (j+1),
                           print ("]"),
                           print(":"),
                           print("-")

1 个答案:

答案 0 :(得分:0)

我想通过简单地添加j> i

来实现它