In python, how does one use listoflists to represent a matrix?

Source: Internet
Author: User
I found a small problem when I click leetCode. & gt ;_& lt; I don't know why. Please help me to see it ~~ Python initializes alistoflistsofinteger by using the following method (assuming a 4x4 matrix): n4matrix [[[0] * n] * nprintmatrix [[0, 0, 0], [0, 0, 0], [0, 0, 0, 0], [0, 0, 0 when the subject clicks leetCode, a small situation found >_< do not know why, please kindly help me to see ~~

When python initializes a list of lists of integer, it uses the following method (assuming a square matrix of 4x4 ):
N = 4
Matrix = [[0] * n
Print matrix
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0], [0, 0, 0, 0]
The strange thing is, if I just want to assign values to the two elements in the middle of the second line:
Matrix [1] [1: 3] = [1, 2]
The result is:
Print matrix
[[0, 1, 2, 0], [0, 1, 2, 0], [0, 1, 2, 0], [0, 1, 2, 0]

However, if you initialize the matrix as follows:
Matrix = [[0 for col in range (n)] for row in range (n)]
Matrix [1] [1: 3] = [1, 2]
Print matrix
[[0, 0, 0, 0], [0, 1, 2, 0], [0, 0, 0], [0, 0, 0, 0]
It will be the answer I want.

But I don't know where the first initialization method is wrong... reply content:

matrix = [[0]*n]*n

#! /Usr/bin/python # encoding = UTF-8 # content from Chapter 4.5 of python cookbook 2 # list creation without sharing references # Avoid implicitly referencing shared multi = [[0] * 5] * 3 print multi [0] [0] = 'oposs' print multi # [['oposs ', 0, 0, 0, 0], ['oops', 0, 0, 0, 0], ['oops', 0, 0, 0, 0, 0] # equivalent method: row = [0] * 5 # all five sub-items in the row List reference 0 multi = row * 3 # All three sub-items in the multi List reference row # Explanation: during row creation, it is not important to copy a reference because the referenced # Is a number and the number cannot be changed. In other words, if the object cannot be changed, then # There is no difference between the object and the reference to the object. # In multi creation, three [row] content references are contained, and the content is a reference to a # list. Therefore, the other three references are changed, and even the row is changed. # solution: multilist_method1 = [[0 for col in range (5)] for row in range (3)] multilist_method2 = [[0] * 5 for row in range (3)] multilist_method1 [0] [0] = 'abc' multilist _ method2 [0] [0] = 'def' print multilist_method1print multilist_method2
Agree with the above steps. In addition, NumPy should be used for Matrix-related processing. The first is the shallow copy problem, which is mentioned on p148 of the second version of Python cookbook, that is, implicit reference sharing.
The solution is to use list derivation.
For details, you can read books. (Sorry, I am too lazy to write too many phone code words, hoping to solve your problem .) It's a lot of things to use python for Refresh. Yesterday I met a python division and remainder mechanism different from that of c.

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.