Python uses the list of lists to represent a matrix problem?

Source: Internet
Author: User
The main brush Leetcode when found a small situation >_< do not know what the reason is, please know friends to help see ~ ~

Python Initializes a list of lists of integers, using the following method (assuming a 4 x 4 square):
n = 4
Matrix = [[0]*n]*n
Print matrix
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
And then, oddly, if I only wanted to assign a value to the middle two elements of the second line:
Matrix[1][1:3] = [1, 2]
The result would be:
Print matrix
[[0, 1, 2, 0], [0, 1, 2, 0], [0, 1, 2, 0], [0, 1, 2, 0]]

However, if you initialize the matrix in the following way:
Matrix = [[0 for Col by 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, 0]]
Will be the answer I want.

But I don't know where the first type of initialization is wrong ...

Reply content:

matrix = [[0]*n]*n
#!/usr/bin/python#encoding =utf-8# Content from Python Cookbook Second edition section 4.5# Create a list of lists without sharing references# Avoid implicit reference sharingMulti = [ [0] * 5 ] * 3 Print Multi Multi[0][0] = ' oops 'Print Multi# [[' Oops ', 0, 0, 0, 0],[' oops ', 0, 0, 0, 0],[' oops ', 0, 0, 0, 0]]# Equivalence ModeRow = [0] * 5 # 5 subkeys in the row list are reference 0Multi = Row * 3 # 3 subkeys in the multi list are references to row# Explanation: In row creation, there is no reference being copied completely unimportant, because the referenced# is a number, and the number cannot be changed, in other words, if the object is immutable, then# objects and references to objects are not really different. # Multi is created with 3 references to [row] content, and its content is for a# A reference to the list. As a result, the other 3 references have changed as well, even the row has changed# Workaround:multilist_method1 = [ [ 0  for Col inch Range(5) ]  for Row inch Range(3) ]multilist_method2 = [ [0] * 5  for Row inch Range(3) ]multilist_method1[0][0] = ' abc 'multilist_method2[0][0] = ' EDF 'Print multilist_method1Print multilist_method2
Agree upstairs, in addition to deal with Matrix-related words with NumPy the first is a shallow copy of the problem, in the second edition of the Python Cookbook p148 page mentions, that is, implicit reference sharing problem.
The solution is to use list derivation.
You can read the book in detail. Sorry, I am too lazy to write too much, hoping to solve your problem. Brush Leetcode with Python pit many Ah, yesterday just met a Python division and the remainder mechanism and C is not the same AH
  • 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.