0

I need to input 2d array and make changes in it but the answer is always wrong. This is my code.

r,c,obs = map(int, input().split())
k = []
h = []
for i in range(r):
    for j in range(c):
        k.append(0)
    h.append(k)

for i in range(r):
    for j in range(c):
        print(h[i][j],end=" ")
    print()

while obs > 0:
    l, k = map(int, input().split())
    h[l-1][k-1] = 1
    obs -= 1

for i in range(r):
    for j in range(c):
        print(h[i][j],end=" ")
    print()

My input is this -

5 4 4
3 2
3 4
4 2
5 2

According to the code my resultant matrix should be something like this -

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

But it is always resulting to this -

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

Can anyone tell me what is wrong here??

2 Answers2

3

I guess you forgot to reset k at the end of each iteration of the outer for-loop. How about this?

r,c,obs = map(int, input().split())
h = []
for i in range(r):
    k = []             # This is the only line changed.
    for j in range(c):
        k.append(0)
    h.append(k)

for i in range(r):
    for j in range(c):
        print(h[i][j],end=" ")
    print()

while obs > 0:
    l, k = map(int, input().split())
    h[l-1][k-1] = 1
    obs -= 1

for i in range(r):
    for j in range(c):
        print(h[i][j],end=" ")
    print()

The result is

0 0 0 0 
0 0 0 0 
0 1 0 1 
0 1 0 0 
0 1 0 0 
QQQ
  • 327
  • 1
  • 6
0

EXPLAINATION

your way of printing is worng look at this :

r,c,obs = map(int, input().split())
k = []
h = []
for i in range(r):
    for j in range(c):
        k.append(0)
    h.append(k)
def print_arr(k1):
    for li in k1:
        for it in li:
            print(it,end=' ')
        print()
print_arr(h)
while obs > 0:
    l, k1 = map(int, input().split())
    h[l-1][k1-1] = 1
    obs -= 1
    print_arr(h)

input:

>? 5 4 4
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 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 

>? 3 2
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

also because of the :

for i in range(r):
    for j in range(c):
        k.append(0)
    h.append(k)

what oyu do is assiging the same list to h and it contained the same list for c times and every change you make to any inner list it changes every column (e.g the inner lists contained)

SOLUTION

what would solve the issue is this, change:

for i in range(r):
    for j in range(c):
        k.append(0)
    h.append(k)

to:

for i in range(r):
    k=[]
    for j in range(c):
        k.append(0)
    h.append(k)

output:

>? 5 4 4
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 
>? 3 2
0 0 0 0 
0 0 0 0 
0 1 0 0 
0 0 0 0 
0 0 0 0 
adir abargil
  • 5,495
  • 3
  • 19
  • 29
  • @SAM GARAI - you did not create a 2D matrix as you will think. You could change the lines to these: (after k = []; h = [] ) And try it out. From your original code, you just create 1D array h - that reference to the same array k. ```python k = [0 for i in range(c) for i in range(r)]; h = [[0 for i in range(c)] for i in range(r)] – Daniel Hao Dec 20 '20 at 13:16