0
m = 4
n = 3
flags = [[0]*n]*m
for i in range(m):
    for j in range(n):
        print(str(i)+","+str(j)+": ",flags)
        flags[i][j] = 1
        print(str(i)+","+str(j)+": ",flags)

The result is as follow.
However, my expectation on the second line is "0,0: [[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]"
Please correct my understanding

=> python3 test.py
0,0: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
0,0: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]
0,1: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]
0,1: [[1, 1, 0], [1, 1, 0], [1, 1, 0], [1, 1, 0]]
0,2: [[1, 1, 0], [1, 1, 0], [1, 1, 0], [1, 1, 0]]
0,2: [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
1,0: [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
1,0: [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
... 3,2: [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
3,2: [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]

Asked a question related to programming in python

heejean
  • 11
  • 1

0 Answers0