0

in 19x19 matrix with all components == 0 now (list of list a)

for example, i want to change a[1][1] to 1

but they changed all index(1) of lists of list(a)

emphasized text tried to stop using for- sentence and changed to while-, just typed several times not using loops.

a = []

row = []

for i in range(19) :
    row.append(0)

for i in range(19) :
    a.append(row)

#19x19 matrix a has been made

n = int(input())

for j in range(n) :
    x, y = map(int, input().split())
    a[x-1][y-1] = 1


for k in a :
    print(k)
busybear
  • 10,194
  • 1
  • 25
  • 42
위태영
  • 11
  • 1

1 Answers1

3

You are adding the same row again and again.

a = []

row = []

for i in range(19) :
    row.append(0)

for i in range(19) :
    a.append(row[:])    #slice it to make a copy
Ashwani
  • 1,938
  • 11
  • 15
  • Just thinking, it might be more helpful for OP to understand the concept if you can add some explanation to why they are the same row. – TYZ Aug 23 '19 at 14:16
  • thanks !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! – 위태영 Aug 23 '19 at 15:10