A detailed description of how Python represents a matrix

Source: Internet
Author: User
This article mainly introduced the Python expression matrix method, combined with the concrete example form to analyze the Python expression matrix method and the related operation attention matters, the need friend can refer to the next

The example in this article describes how Python represents a matrix. Share to everyone for your reference, as follows:

In the C language, a matrix representing "Integer 3 rows and 4 columns" can be declared as follows: Int a[3][4]; in Python, you cannot declare a variable int, and two cannot list the number of dimensions. Can be represented by a list of entrained lists. For example:

Represents a matrix, so you can:

Count = 1a = []for i in range (0, 3):  TMP = [] for  J in range (0, 3):    tmp.append (count)    count + = 1  a.app End (TMP) print a

Results:

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

But note: initialization (Assignment is all 0 o'clock), the following is wrong!!

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

Results:

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

Cause: This list of TMP for the same, change any row, the other lines will be given to change, pay attention!! , the following is correct:

A = []for i in range (0, 3):  TMP = [] for  J in range (0, 3):    tmp.append (0)  a.append (TMP) print a
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.