Transpose a matrix in Single line in Python
Transpose of a matrix is a task we all can perform very easily in python (Using a nested loop). But there are some interesting ways to do the same in a single line.
In Python, we can implement a matrix as nested list (list inside a list). Each element is treated as a row of the matrix. For example m = [[1, 2], [4, 5], [3, 6]] represents a matrix of 3 rows and 2 columns.
First element of the list – m[0] and element in first row, first column – m[0][0].
- Using Nested List Comprehension: Nested list comprehension are used to iterate through each element in the matrix.In the given example ,we iterate through each element of matrix (m) in column major manner and assign the result to rez matrix which is the transpose of m.
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)
chevron_rightfilter_noneOutput:
[1, 2] [3, 4] [5, 6] [1, 3, 5] [2, 4, 6]
- Using zip: Zip returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. In this example we unzip our array using * and then zip it to get the transpose.
matrix
=
[(
1
,
2
,
3
),(
4
,
5
,
6
),(
7
,
8
,
9
),(
10
,
11
,
12
)]
for
row
in
matrix:
print
(row)
print
(
"\n"
)
t_matrix
=
zip
(
*
matrix)
for
row
in
t_matrix:
print
(row)
chevron_rightfilter_noneOutput:
(1, 2, 3) (4, 5, 6) (7, 8, 9) (10, 11, 12) (1, 4, 7, 10) (2, 5, 8, 11) (3, 6, 9, 12)
Note :- If you want your result in the form [[1,4,7,10][2,5,8,11][3,6,9,12]] , you can use t_matrix=map(list, zip(*matrix)).
- Using numpy: NumPy is a general-purpose array-processing package designed to efficiently manipulate large multi-dimensional arrays. The transpose method returns a transposed view of the passed multi-dimensional matrix.
# You need to install numpy in order to import it
# Numpy transpose returns similar result when
# applied on 1D matrix
import
numpy
matrix
=
[[
1
,
2
,
3
],[
4
,
5
,
6
]]
print
(matrix)
print
(
"\n"
)
print
(numpy.transpose(matrix))
chevron_rightfilter_none
This article is contributed by Mayank Rawat .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Multiplication of two Matrices in Single line using Numpy in Python
- Python | Pandas Series.transpose()
- Python | Pandas TimedeltaIndex.transpose()
- A single neuron neural network in Python
- Print Single and Multiple variable in Python
- Python | Replace multiple occurrence of character by single
- Python | Convert a list of multiple integers into a single integer
- Python code to move spaces to front of string in single traversal
- Multi-Line printing in Python
- Command Line Interface Programming in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Python | Set 6 (Command Line and Variable Arguments)
- How to input multiple values from user in one line in Python?
- Line detection in python with OpenCV | Houghline method
- Python | Plotting Line charts in excel sheet using XlsxWriter module
Improved By : pwakchaure