January 24, 2018: LABA Chronicle and 2018: LABA Chronicle
Reading a book today, old tie sent me an interview question.
When I first read this question, I think it is to score the question. Write a line of command at the top of the corner of the mouth:
print(list((i for i in a if '7net' not in i)))
After taking a closer look, this question is about deleting the characters, so that he is at risk and then changed it.
an=(''.join(i.split('7net')) for i in a)print(list(an))
After reading it, I sent it to la, and the old iron replied instantly: neither nor. Do you dare to ask me for this difficulty. There is a trap in this question. If a list Derivation or generator expression is generated or a map, filter, or ternary expression is changed, the original list is generated to generate a new list.
I did not expect a small interview question to hide the xuanjicang. I tried the map function and the result is as follows:
print(id(a[2]))xx=map(lambda x:x if '7net' not in x else ''.join(x.split('7net')) ,a)print(iter(xx))print(iter(xx))print(id(iter(xx)))
We can see that the id address of the third parameter in the list has changed, and this value does not contain '7net'. Therefore, the list is not the original list (or the new list ).
Here, I think only the delete method of the list can be used to complete the in-situ modification.
nn= (a.remove(i[1]) for i in list(enumerate(a)) if '7net' in i[1])print(list(a))
The remove in the generator expression does not take effect and the original list is not changed.
But it is okay to put it in list derivation.
print(id(a))nn= [a.remove(i[1]) for i in list(enumerate(a)) if '7net' in i[1]]print(list(a))print(id(a))
print(id(a))nn=[a.remove(i) for i in a if '7net' in i]print(a)print(id(a))
The list is different from the generator. Modifying in the for loop will lead to index moving forward and deletion of elements.
But this is not the result we need. What we need is to modify the string on the original basis rather than remove it.
''. Join (I. split ('7net'), strings cannot be modified in the same place. Therefore, the list will be changed.
I have basically concluded that this question cannot be written. Because the list in python is actually a storage sequence of Element Memory addresses, the list only stores the actual storage address of an address space similar to a pointer pointing to an internal element. However, if we generate a new string that directs the memory address to a new value, and the new value needs to open up a new memory space for storage, we only need to replace the New String, the current list must not be the original list.
Too young, simple, above all I mentioned.
During dinner, I posted a piece of code. Take a closer look and copy a. At this time, a is still the sequence of all the memory addresses before, pointing the original addresses one by one to the new data, then the list has been modified in the original place.
This time it seems there should be no problem.
print(id(a))a[:]=map(lambda x:x if '7net' not in x else ''.join(x.split('7net')) ,a)print(id(a))print(a)
print('0',id(a[0]))print('1',id(a[1]))print('2',id(a[2]))print(id(a))print('--------------------------')a[:]=map(lambda x:x if '7net' not in x else ''.join(x.split('7net')) ,a)print('0',id(a[0]))print('1',id(a[1]))print('2',id(a[2]))print(id(a))print(a)
Although the address of the storage string has changed, the address of the storage list is still the original address, so the original address is modified.
The correct solution is as follows:
a[:]=[i.replace('7net','') if '7net' in i else i for i in a]
Yes:
a[:]=map(lambda x:x if '7net' not in x else ''.join(x.split('7net')) ,a)
To sum up, there is no simple problem. If you think it is simple, you are too simple.
Please correct me.