0
what is the significance of this line in matrix: rez = [[m[j][i] for j in range (len(m))] for i in range(len(m[0]))]
m = [[1,2],[3,4],[5,6]] for row in m : print(row) rez = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))] print("\n") for row in rez: print(row)
2 Réponses
+ 1
I am new to python but I will try 😅
m[j][i]
j is from 0 to 2 so it will get all the the elements inside the main matrix
i 0 and then one it will take you over the elements inside the inner matrix
when you combine them with for you will get all the elements which are numbers 1 2 3 4 5 6
0
# it is regrouping the values
# from the vertical rows.
matrix = [[1,2],
[3,4],
[5,6]]
width = len(matrix[0])
height = len(matrix)
rez2 = [[matrix[j][i] for j in range(height)] for
i in range(width)]
print(rez2)
# there are alternatives to achieve similar results
rotate = []
for i in range(width):
new_row = []
for j in range(height):
new_row.append(matrix[j][i])
rotate.append(new_row)
print(rotate)
# or
rotate2 = [row for row in list(zip(*matrix))]
print(rotate2)
https://code.sololearn.com/cAP48uSxCWFp/#py