Py学习  »  Python

如何在python中打印对应行和列为零的矩阵的坐标?

Heisenberg • 3 年前 • 1163 次点击  

我需要打印矩阵的坐标,其对应的行和列仅为零

例子:

3 3
1 0 0
0 0 0
1 0 0

在上面的例子中,在坐标(1,1)处,行和列为零(0),我需要打印所有行和列为零的冠状体。(比如需要办理入住手续 加号 )

我的代码:

r,c=map(int,input().split())
l=[list(map(int,input().split())) for i in range(r)]
for i in range(len(l)):
    for j in range(0,len(l[i])):
        if sum(l[i])==0 and sum(l[j])==0:
          print(i,j)

我的代码适用于上面提到的输入,但适用于下面提到的输入不适用为什么??

输入:

6 13 

1 0 1 0 1 0 1 0 1 0 1 0 0     
0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 1 0 1 0 1 0 1 0 1 0

所需输出:

1 13
2 13
3 13
4 13

我的输出:

1 1
1 2
1 3
1 4

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    if sum(l[i])==0 and sum(l[j])==0:
IndexError: list index out of range

我犯了什么错误?请帮帮我!!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133378
 
1163 次点击  
文章 [ 1 ]  |  最新文章 3 年前