Python - transpose of a matrix

A friend raises the question: is now on hand a two-dimensional list, such as [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ], is now put into the two-dimensional list of [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]].

In fact, no brains, then use the double loop is easy to write:

# ! / Usr / bin / env python3 
# - * - Coding: UTF-8 - * - 

# Author: Mayi 
# Blog: http://www.cnblogs.com/mayi0312/ 
# a Date: 2019/4/26 
# the Name : Test01 
# Software: PyCharm 
# Note: for implementing of a matrix (doublet list) transpose 


DEF Trans (L): 
    A = [[] for I in L [0]]
     for I in L:
         for J in Range ( len (I)): 
            A [J] .append (I [J]) 

    return A 

# main function 
DEF  main ():
    L1 = [[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9], [10,. 11, 12 is ]] 

    L2 = Trans (L1)
     Print (L1) # [[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9], [10,. 11, 12 is]] 
    Print (L2) # [[. 1,. 4,. 7, 10], [2,. 5, 8, 11], [3, 6, 9, 12]] 

# entry function 
IF  the __name__ == ' __main__ ' : 
    main ()

Yet no matter how look at this code is ugly.

As Figure: immediate sense that the transposed matrix of how the matter?

Yes, the nature of the problem is how to solve the transposed matrix. So it is simple, with one or unthinking way:

# ! / Usr / bin / env python3 
# - * - Coding: UTF-8 - * - 

# Author: Mayi 
# Blog: http://www.cnblogs.com/mayi0312/ 
# a Date: 2019/4/26 
# the Name : Test01 
# Software: PyCharm 
# Note: for implementing of a matrix (doublet list) transpose 


DEF Trans (L):
     for I in Range (len (L)):
         for J in Range (I): 
            L [I] [J], L [J] [I] = L [J] [I], L [I] [J] 

    return L 

# main function 
DEF main (): 
    L1= [[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9 ]] 

    L2 = Trans (L1)
     Print (L1) # [[. 1, 2,. 3], [. 4,. 5 ,. 6], [. 7,. 8,. 9]] 
    Print (L2) # [[. 1,. 4,. 7], [2,. 5,. 8], [. 3,. 6,. 9]] 

# entry function 
IF  the __name__ == ' __main__ ' : 
    main ()

In fact, the above code is still a little bug, appears to be useful, however, this method requires a matrix ranks the same job length.

Finally, we think of zip. Such is the nature of zip, remove the element corresponding position in the list, the composition of the new list, it is entitled to do this.

So in the end, the subject (transposed matrix) of Python solution is quite fantastic:

# ! / Usr / bin / env python3 
# - * - Coding: UTF-8 - * - 

# Author: Mayi 
# Blog: http://www.cnblogs.com/mayi0312/ 
# a Date: 2019/4/26 
# the Name : Test01 
# Software: PyCharm 
# Note: for implementing of a matrix (doublet list) transpose 


DEF Trans (L): 
    L = ZIP (* L) 
    L = [list (I) for I in L] 

    return L 

# main functions 
DEF main (): 
    L1 = [[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9], [10,. 11, 12 is ]] 

    L2 =Trans (L1)
     Print (L1) # [[. 1, 2,. 3], [. 4,. 5,. 6], [. 7,. 8,. 9], [10,. 11, 12 is]] 
    Print (List (L2)) # [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]] 

# entry function 
IF  the __name__ == ' __main__ ' : 
    main ()

 

Guess you like

Origin www.cnblogs.com/mayi0312/p/11421894.html