In Python, you can use + to connect strings. as we all know, there are actually + connectors in Python. the following article mainly introduces the connectors (+, +) in Python ), for more information, see the following. Preface
This article introduces the connector (+, + =) in Python in detail through the issues found in the sample code. let's take a look at the details below.
Suppose there is the following code:
a = [1, 2, 3, 4]b = [5, 6, 7, 8, 9]c = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] for item in (a, b, c): item += [0] * (10 - len(item)) print aprint bprint c
This code indicates that there are three lists. you need to fill in 0 at the end of the list with a length of less than 10 to change its length to 10.
The output is as follows:
[1, 2, 3, 4, 0, 0, 0, 0, 0, 0][5, 6, 7, 8, 9, 0, 0, 0, 0, 0][11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
There is no problem here. everything is normal. However, the requirement has changed. you need to fill 0 in front of the list with a length of less than 10.
Then, we try to make the following changes:
a = [1, 2, 3, 4]b = [5, 6, 7, 8, 9]c = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] for item in (a, b, c): item = [0] * (10 - len(item)) + item print aprint bprint c
Let's take a look at the output:
[1, 2, 3, 4][5, 6, 7, 8, 9][11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
The results are not what we think. If you have not found the problem, continue. Of course, if you have seen the clues, you do not need to waste time here.
According to our inherent thinking, the above method is feasible, for example, the following example:
>>> l = [1, 2, 3, 4, 5]>>> l = [0]*5 + l>>> l[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]
This operation allows the list to be changed as expected.
However, what if we add several more steps:
>>> l = [1, 2, 3, 4, 5]>>> id(l)139935500860952>>> l = [0]*5 + l>>> l[0, 0, 0, 0, 0, 1, 2, 3, 4, 5]>>> id(l)139935500783272
So far, have you seen the problem. The output of the id () method shows that the "l" behind is no longer the "l" in the front.
Let's take a look at the example below:
>>> l = [1, 2, 3, 4, 5]>>> id(l)139935500861024>>> l += [0]*5>>> l[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]>>> id(l)139935500861024
When + = is used, there is one before and after "l. At this point, we should understand the fact that the example at the beginning of the article is not inexplicable, but for a reason.
Don't worry. let's take a look at the example:
>>> t = (1, 2, 3, 4, 5)>>> id(t)139935501840656>>> t += (0,)*5>>> t(1, 2, 3, 4, 5, 0, 0, 0, 0, 0)>>> id(t)139935502151336
We can see that when we replace the list with a tuples, the results change again.
Then we use + operations for tuples:
>>> t = (1, 2, 3, 4, 5)>>> id(t)139935501081200>>> t = (0,)*5 + t>>> t(0, 0, 0, 0, 0, 1, 2, 3, 4, 5)>>> id(t)139935502151336
This is the same as the list result.
Then, let's take a look at the string:
>>> s = "hello">>> id(s)139935500909712>>> s += "world">>> s'helloworld'>>> id(s)139935500909664
The result is like a tuples. "s" is re-assigned after a string is concatenated by "+ =", which is no longer the previous variable. In the memory, "s" is opened into another bucket to store values.
Here, the Python connector is + and + =. Note that the two symbols in Python have meanings. one is the addition operation in mathematics, and the other is the concatenation function used in the sequence type. However, as an addition operator, we also follow the rules discussed in this article. Because these two symbols are discussed, they are essentially the immutable and mutable of Python, that is, the variable type and the immutable type. For a mutable type, we can modify it in the original variable, that is, its storage space is readable and writable, such as list; for an immutable type, its bucket is read-only and cannot be modified. if you need to perform some operations on the immutable type to get new results, A new bucket is required to store the new results.
From the examples listed above, we can draw the following conclusions:
For variable types:
+: Indicates the connection operation. a new object is created in the result.
+ =: Indicates the append operation, that is, the in-place operation, which appends the content of another object to the object.
For an unchangeable type, both the plus (+) and minus (+) represent the join or sum operation. there is no difference between the two, and the result of the operation produces a new object.
Next we will analyze the example at the beginning of the article. because for iteration is equivalent to assigning values, we will analyze only a for the sake of simplicity, as shown below:
>>> a = [1, 2, 3, 4]>>> t = a>>> id(a)139712695835400>>> id(t)139712695835400>>> t += [0]*6>>> t[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]>>> id(t)139712695835400>>> id(a)139712695835400>>> a[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]>>>>>>>>> a = [1, 2, 3, 4]>>> t = a>>> id(a)139712695835464>>> id(t)139712695835464>>> t = [0]*6 + t>>> t[0, 0, 0, 0, 0, 0, 1, 2, 3, 4]>>> a[1, 2, 3, 4]>>> id(a)139712695835464>>> id(t)139712695835400
Here, t is a reference to a, which is equivalent to the item in the example at the beginning of the article. Using + = to operate on t is actually to operate on a, while + = is an in-situ operation, so when t is changed, a also changes; if you use + to operate on t, when the result is assigned to t, t no longer points to a, but to [0] * 6 + t, so a is not changed.
Summary
The above is all the content of this article. here we are only discussing a simple question, but I have used such a long space to talk about this question. so what I want to say is, if you do not fully understand these small issues, it may cause you a lot of trouble in the programming process.
For more details about the connector (+, + =) examples in Python, please follow the PHP Chinese network!