The heap and stack of the Python memory space and the change logic of the list

   I introduced the relationship between the heap and the stack of the Java storage space before, and the introduction of Python today actually has the same principle. An object will request a piece of space in memory to save data. When accessing an object, it will not directly access the data of the object in memory, but access it by reference.


   The following will use the Python list (list) as an example. It is very similar to Java's array representation, except that list can have collections of different types.

insert image description here



lst1 = [1,2,3]
lst2 = lst1

print('原始的lst1:',lst1)
print('原始的lst2:',lst2,'\n')

lst2[0] = 666

print('更改后的lst1:',lst1)
print('更改后的lst2:',lst2)


   Here is another example of deleting elements from a typical list. Suppose there is lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we need to delete all 10 elements in it, and finally keep an empty list . Maybe everyone will think of using the loop method to realize it at the first time. Test the results first.
   It can be seen that the final result is [2,4,6,8,10], what is the reason?

When i = 1, lst.remove(i), remove 1, (i is equivalent to lst[0], execute del lst[0]) lst = [2,3,4,5,6,7,8,9 ,10]


When i = 2, lst.remove(i) cannot be understood as remove 2, (i is actually equivalent to lst[1], execute del lst[1]), lst = [2,4,5,6,7 ,8,9,10]

When i = 3, lst.remove(i) cannot be understood as remove 3, (i is actually equivalent to lst[2], execute del lst[2]), lst = [2,4,6,7,8 ,9,10] , and so on, the final result is [2,4,6,8,10]



insert image description here



   Therefore, if you want to perfectly match and completely clear all the elements in the list, you can use slicing or copying to achieve it.

   slice method



insert image description here



lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in lst[:]:
    lst.remove(i)

print(lst)


   copy list method



insert image description here



lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in lst.copy():
    lst.remove(i)

print(lst)


   PS: As in the above example, you can directly delete the odd numbers in the list, and you can also directly delete the even numbers in the list.



insert image description here



lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in lst:
    lst.remove(i+1)

print(lst)


   Of course, it can also be realized by adding a remainder condition judgment.



insert image description here



lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in lst:
  if i % 2 != 0:
    lst.remove(i)

print(lst)


insert image description here



lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in lst:
  if i % 2 == 0:
    lst.remove(i)

print(lst)


   The above briefly introduces the Python heap and stack, as well as typical examples of changes in the list, which are often encountered when writing test cases in software testing. As long as you understand the corresponding logical principles, you can flexibly apply them to test cases.

Guess you like

Origin blog.csdn.net/weixin_48591974/article/details/129857919