Why Does The Input In The Array Overwrites The Values Of Each Row
i have this piece of code x=3 a=x*[x*[0]] for i in range(0,x): for j in range(0,x): dt=int(input('insert data: ')) a[i][j]=dt print(a) and it's supposed t
Solution 1:
you just created 3 rows with the same reference with a=x*[x*[0]]
. x*[0]
is built once, and propagated on all rows by the outer multiply operator.
Changing a row changes all the rows. Note that it can be useful (but not there obviously)
Do that instead (using a list comprehension):
a=[x*[0] for _ in range(x)]
so references of each row are separated
Post a Comment for "Why Does The Input In The Array Overwrites The Values Of Each Row"