Beware of the * Repetition (operator) in Python

Source: Internet
Author: User
In Python there is a special symbol "*" that can be used as a multiplication operator for numeric operations and as a repetition operator for objects, but must be noted when used as a repeating operator

Note: * Repeated objects have the same ID, that is, pointing to the same address in memory, it is important to note that each object is manipulated.

For example:

>>> alist = [Range (3)]*4>>> alist[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]

The above initializes a two-level list to simulate the matrix, the Matrix 4x3, for the convenience of description, here the matrix is a.

Now I want to assign a value of 1 to A11, using the following code:

1

Alist[0][0]=1

So the result we want is:

1

[[1, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]

But unfortunately, what we get is:

1

[[1, 1, 2], [1, 1, 2], [1, 1, 2], [1, 1, 2]]

What is this, why give A21, why the other Ai1 have changed?


The reason is this:

At the beginning of the article we have said, * repeated objects have the same ID, that is, pointing to the same address in memory, it is important to note the operation of the individual objects.

When we initialize again, we use the repetition operator "*", which, when repeating the object, points all the duplicates to the same memory address, all when you change one of these values.

Other values are naturally updated, and the following commands and outputs are explained in Python:

>>> ID (alist[0]) 18858192>>> ID (alist[1]) 18858192>>> ID (alist[2]) 18858192>>> ID (Alist[3]) 18858192>>>

See, the IDs are all the same, which means the 4 lists are the same "list".

In that case, what do we want to do with a matrix, in addition to the special NumPy package, you can certainly give the upper list a append new list, for example:

>>> blist=[]>>> for I in range (4):    blist.append ([J to J in Range (3)]) >>> blist[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]

So, let's try the assignment above:

>>> blist[0][0]=1>>> blist[[1, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]>>>
  • 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.