Python represents the Matrix Method Analysis, python represents the Matrix

Source: Internet
Author: User

Python represents the Matrix Method Analysis, python represents the Matrix

This example describes how to use Python to represent a matrix. We will share this with you for your reference. The details are as follows:

In C language, it indicates a matrix of "Three integer rows and four columns". It can be declared as follows: int a [3] [4]; in python, the variable int cannot be declared, 2. dimensions cannot be listed. You can use the list to enclose the list. For example:

Indicates a matrix, which can be as follows:

Count = 1a = [] for I in range (0, 3): tmp = [] for j in range (0, 3): tmp. append (count) count + = 1. append (tmp) print

Result:

[1, 2, 3], [4, 5, 6], [7, 8, 9]

Note that initialization (when all values are 0) is incorrect !!

Tmp = [] for j in range (0, 3): tmp. append (0) a = [] for I in range (0, 3):. append (tmp) print

Result:

[[0, 0, 0], [0, 0, 0], [0, 0, 0]

Cause: the tmp in this list is the same. If any row is changed, other rows will be changed. Please pay attention to it !!, The following are correct:

A = [] for I in range (0, 3): tmp = [] for j in range (0, 3): tmp. append (0). append (tmp) print

Related Article

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.