Implementation of matrix inversion under Python

Source: Internet
Author: User
Python under the matrix upside down, you can use a double loop, or a zip implementation.

  1. Loop implementation:

Nested list derivation

Matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
matrix_t = [[Row[col] for row in matrix] for Col in range (len (matrix[0))]
matrix_t
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

The principle is simple: loop the columns first, looping through each row on a fixed column.

  1. Implemented in Zip (Python3):

    Matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
    matrix_t = List (map (list, zip (*matrix)))
    matrix_t
    [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

The principle is also clear: first iterate through each list in parallel with zip, and then use map to turn the iterated tuple into a list.

Implementation of matrix inversion under Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.