In this tutorial, you'll learn how to transpose a matrix in Python using 4 ways.
—
Facebook: [ Ссылка ]
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn four ways to transpose a matrix in Python. Number one, using a nested for loop.
On line three, I have a three-by-three matrix represented by a list of lists. Online seven, I have a nested for loop where the I index is looping over the rows and the J index is iterating over the columns. Line 9, I'm interchanging the I and J indices to transpose the matrix.
Let's execute this program to see what the output looks like. Number two, you can also use a nested list comprehension syntax to replicate exactly the same thing that we just did but in one line. Number three, using the NumPy library.
Here on line 20, I'm using the numpy.transpose method to transpose our original matrix. Last but not the least number four, uses the zip and the map function. On line 25, I'm unzipping our original matrix and using the map function to map the return tuple into a list.
There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.
Connect MySQL In Python: [ Ссылка ]
Run Selenium PyTests In Parallel: [ Ссылка ]
find_elements() in Selenium: [ Ссылка ]
—
import numpy
# 1. Using nested for loop
matrix = [['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']]
matrix1 = matrix
for i in range(len(matrix)):
for j in range(len(matrix[0])):
matrix1[i][j] = matrix[j][i]
print("matrix1:", matrix1)
# 2. Using nested list comprehension
matrix2 = [[matrix[j][i] for i in range(len(matrix))] for j in range(len(matrix[0]))]
print("matrix2:", matrix2)
# 3. Using numpy
matrix = [['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']]
matrix3 = numpy.transpose(matrix)
print("matrix3:")
print(matrix3)
# 4. Using zip & map
matrix4 = map(list, zip(*matrix))
print("matrix4:")
[print(_) for _ in matrix4]
--
Video Transcript
--
Hi guys, this is Abhi from Gokce DB. In this video, you’re going to learn Four Ways to Transpose a Matrix in Python.
Number one using a nested for loop. On line three, I have a three by three matrix represented by list of lists.
On line seven, I have a nested for loop where the I index is looping over the rows and the J index is iterating over the column. Line nine, I’m interchanging over the I and J in the C’s to transpose the matrix.
Let’s execute this program to see how the output looks like. Number two, you can also use a nested list comprehension syntax to replicate exactly the same thing or we just did but in one line.
Number three using the numpy library. Here, on line twenty I’m using the numpy dot transpose method to transpose our original matrix.
Last, but not the least, number four using the zip and the map function. On line twenty-five, I’m unzipping our original matrix and using the map function to map the return to for into a list.
There you have it. Make sure you like, subscribe, and turn on the notification bell.
Until next time.
4 Ways To Transpose Matrix In Python (1 Min)
Теги
python matrixtranspose of a matrixmatrix transpose in pythonpython tutorialpython matrix transposematrix in pythontranspose of matrix pythonpython transpose of matrixnumpy tutorialpython programmingpython tutorial for beginnerstranspose matrix in pythontranspose of a matrix in pythonnumpy transpose arraytranspose a matrix in pythontranspose a matrix pythontranspose of matrix in python using numpynested listpython programlist as matrix