The confusion lies in the Python Programming (2nd edition) Dong Tsinghua University Press, page 46th
The original text is:
"List-derived">>>freshfruit=['Banana','Loganberry','Passion Fruit']>>>alist=[w.strip () forWinchFreshfruit] is equivalent to the following code:>>>freshfruit=['Banana','Loganberry','Passion Fruit']>>> forI, VinchEnumerate (Freshfruit): Freshfruit[i]=V.strip () is also equivalent to>>>freshfruit=['Banana','Loganberry','Passion Fruit']>>>freshfruit=list (Map (Str.strip, Freshfruit))
The key puzzle point is the map (Str.strip, Freshfruit) in the last line of code
According to the previous essay, the map (A, B) function is to return the result of B into A, according to this, then the original text should be the elements in the list freshfruit into the Str.strip , but what is str.strip ? !
Let's take a look at what the results of these code outputs are:
['banana'loganberry'passion fruit ']
The result is the same as the original list (Freshfruit) content
Judgment ID (freshfruit) = = ID (alist) the answer is False
It is possible to know that this code is a copy of the contents of the list , not the copy of the memory address , that is, not the "is" relationship, but the "= =" relationship.
Here you can understand that copying a list cannot be as simple as a = B, because this is the equivalent of the memory address, and changing one of the lists will also change the other list.
For example:
>>> oldlist = ['a','b','C']>>> NewList =oldlist>>>oldlist['a','b','C']>>>newlist['a','b','C']>>> Newlist.append ('D')>>>newlist['a','b','C','D']>>>oldlist['a','b','C','D']
Changing the newlist will also change the oldlist.
>>> ID (oldlist)1625304>>> ID (newlist) is newlisttrue
Memory address.
Then back to the original doubts, Str.strip exactly what is it?
It dawned on me that I realized, according to the strip () function, the Python strip () method is used to remove the character (by default, a space or line break) or sequence of characters specified by the tail of the string. --by Rookie Tutorial
And that piece of code in the book is actually like this:
>>>freshfruit=[' loganberry ' 'passion fruit ']
There are a few spaces before and after the character, not the same as the code I started knocking on.
★ This explains the fact that the code in the book means to remove the space , not the copy of the list content I thought of ★
Doubts solved!
Beginners Learn Python puzzle (i)--Copy of list content