Python easy-to-error blind spot Troubleshooting the difference between + + + and some assignment operations stepped on the pit

Source: Internet
Author: User

question 1. int and list are not the same

>>> a=1>>> b=a>>> a+=1>>> A, B (2, 1)>>> a=[ 1,2,3,4]>>> b=a>>> a+=[5]>>> A, b ([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])

In layman's words, A and B are "different" when the type is int, and A and B are "the same" when the type is list. The term is called immutable and mutable, and the specific principle does not need to be delved into this node.
question 1.1. When we run the B=a statement, we intuitively assume that B and a are different.

>>> a=[[1],[2],[3],[4]]>>> B+=a[0:2]>>>b[1, 2, 3, 4, [1], [2]]>>> a=[[1],[2],[3],[4]]>>> b=[]>>> B+=a[0:2]>>>A , B ([[1], [2], [3], [4]], [[1], [2]])>>>b[0][1]>>> b[0][0]='changed!'>>>#You don ' t expect a to the change>>>#however>>>A, B ([['changed!'], [2], [3], [4]], [['changed!'], [2]])

As you can see, [1] and b[0] [1] are "the same", because changing the b[0] will change a[0] (note that it is not a change of B, it is a change of the b[0]). Change B will not have any effect on a)
question 2. In the case of list, a+=b and a=a+b are not the same :

>>> a=[1,2,3,4]>>> b=a>>> a+=[5]>>> B ([1 , 2, 3, 4, 5], [1, 2, 3, 4, 5])>>> a=[1,2,3,4]>>> b=a>>> a=a+[5  ]>>> A, b ([1, 2, 3, 4, 5], [1, 2, 3, 4])

Similarly, in the case of + =, A is the original A, and B "the same"; in the case of +, A is not the original A, and B "is not the same."
question 3. What should I do if we want to make the + + and + behavior consistent?

Import copy>>> a=[1,2,3,4]>>> b=copy.deepcopy (a)>>> a+=[5] >>> A, b ([1, 2, 3, 4, 5], [1, 2, 3, 4])

This is consistent with the results of a=a+b in question 2. When a list is b=a, it is actually a "reference" operation, and only using B=copy.deepcopy (a) is the "copy" operation we normally expect.
question 4. Back to the code in question, when k=1, the following code:

subset + = (Elements[0:size])

Depending on the problem 1.1,subset and elements are "the same", so future changes to subset elements may change the elements of elements .

In this line of code, note that set is a recursive pass-through subset:

# Set[j] + =  (Elements[i])  #Why Elements? SET[J]  = set[j] +  

According to the question 2,+= Set[j] is still the original set[j], it may be the element of elements. So

SET[J] + = Elements[i]

may be equivalent to

Elements[*] + = Elements[i]

Once the elements of the elements are changed, the result is naturally wrong.
How to solve this problem? According to question 3, as long as the set and elements are "not the same", it conforms to the logic of the program. Therefore the

subset + = (Elements[0:size])

Change (remember import copy)

subset + = Copy.deepcopy (Elements[0:size])

will be able to operate normally in the case of + =.
Summary: in Python, the assignment of the list type B=a the reference operation, not the copy operation, which needs to be added b=copy.deepcopy (a) When the copy operation is required. (The difference between copy.copy and copy.deepcopy is beyond the scope of the problem, interested can Google)

Python easy-to-error blind spot Troubleshooting the difference between + + + and some assignment operations stepped on the pit

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.