Apr-30-2019, 01:17 PM
Hello Everyone,
I am new in Python programming and trying to transpose the matrix. So if the matrix is
the output will be
Here is my code:
If I already append the value of t in k before clearing the content of t, then print(k) should print the correct values instead blank. Why t.clear() causes printing null values inside the list?
Could you please explain what I missed?
I am new in Python programming and trying to transpose the matrix. So if the matrix is
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],]
the output will be
# Transpose output [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
.Here is my code:
t=[] k=[] for x in range(0,4): #range(0,3) [y,x] for y in range(len(matrix)): t.append(matrix[y][x]) k.append(t) #t.clear() #seems issue t=[] print(k)To clear the content of list t, initially I used t.clear() which causes the output becomes
Output:[[], [], [], []]
Alternatively, I replace it with the t=[] and it works perfectly providing output Output:[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
As per the Python documentation, t.clear() "remove all items from the list and equivalent to del a[:]". If I already append the value of t in k before clearing the content of t, then print(k) should print the correct values instead blank. Why t.clear() causes printing null values inside the list?
Could you please explain what I missed?