In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the difference between "extend (),"+" and "+ =" in Python. In daily operation, I believe many people have doubts about the difference between extend (), "+" and "+ =" in Python. The editor has consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "extend (),"+" and "+ =" in Python! Next, please follow the editor to study!
Preface
When using Python, we know that list is an array of variable length pairs, and the number of elements can be easily expanded through insert,append and extend. You can also use operators such as: [1] + [2] to generate a new array [1,2]
The difference between extend (), "+" and "+ ="
"+" adds two list and returns a new list object
Append is modified on the original list and no value is returned
As you can see from the following code, after calling b = b + [3,4], you can see that b becomes a new object through id (b).
In [5]: B = [1,2] In [6]: id (b) Out [6]: 1628740249224 In [7]: B = b + [3,4] In [8]: id (b) Out [8]: 1628740456520
Using extend () to complete the same steps, you can see that the id of object c remains the same as the original
In [9]: C = [1,2] In [10]: id (c) Out [10]: 1628740392584 In [11]: c.extend ([3,4]) In [12]: id (c) Out [12]: 1628740392584
Using the "+ =" connection list, you can see that the effect is the same as extend ().
In [1]: a = [1,2] In [2]: id (a) Out [2]: 1628740021448 In [3]: a + = [3,4] In [4]: id (a) Out [4]: 1628740021448
Conclusion: the syntax of list1 = list1 + list2 should be avoided when reducing the copy of memory and modifying the data of a list.
Memory usage of List
An example:
In [1]: import sys In [2]: lst1 = [1] In [3]: lst2 = [] In [4]: lst2.append (1) In [5]: lst1 = = lst2Out [5]: True In [6]: sys.getsizeof (lst1) Out [6]: 72In [7]: sys.getsizeof (lst2) Out [7]: 96
You can see that lst1 = = lst2, but when you use sys.getsizeof to get the memory size of an object, the two are different.
As shown in the following figure, the length of list_a is 4. When append (4) is executed, the underlying data length actually applies for space of four elements. When append (5) is executed again, there is no need to request memory again.
Because when you perform the append () operation, Python expands the memory of N elements at a time, because one append operation is likely to be the beginning of many append operations, reducing the number of possible memory allocations and memory copy by allocating additional memory.
In [1]: import sys
In [2]: l = []
...: print (f'list initial size {sys.getsizeof (l)}')
For i in range (80):
: cur_size = sys.getsizeof (l)
...: l.append (I)
: new_size = sys.getsizeof (l)
: print (f'list len {new_size 1}:\ t current_size {new_size}\ t new_allocated 8 * {(new_size-cur_size) / 8}')
...:
List initial size 64
List len 1: current_size 96 new_allocated 8 * 4.0
List len 2: current_size 96 new_allocated 8 * 0.0
List len 3: current_size 96 new_allocated 8 * 0.0
List len 4: current_size 96 new_allocated 8 * 0.0
List len 5: current_size 128 new_allocated 8 * 4.0
List len 6: current_size 128 new_allocated 8 * 0.0
List len 7: current_size 128 new_allocated 8 * 0.0
List len 8: current_size 128 new_allocated 8 * 0.0
List len 9: current_size 192 new_allocated 8 * 8.0
List len 10: current_size 192 new_allocated 8 * 0.0
List len 11: current_size 192 new_allocated 8 * 0.0
List len 12: current_size 192 new_allocated 8 * 0.0
List len 13: current_size 192 new_allocated 8 * 0.0
List len 14: current_size 192 new_allocated 8 * 0.0
List len 15: current_size 192 new_allocated 8 * 0.0
List len 16: current_size 192 new_allocated 8 * 0.0
List len 17: current_size 264 new_allocated 8 * 9.0
List len 18: current_size 264 new_allocated 8 * 0.0
List len 19: current_size 264 new_allocated 8 * 0.0
List len 20: current_size 264 new_allocated 8 * 0.0
List len 21: current_size 264 new_allocated 8 * 0.0
List len 22: current_size 264 new_allocated 8 * 0.0
List len 23: current_size 264 new_allocated 8 * 0.0
List len 24: current_size 264 new_allocated 8 * 0.0
List len 25: current_size 264 new_allocated 8 * 0.0
List len 26: current_size 344 new_allocated 8 * 10.0
List len 27: current_size 344 new_allocated 8 * 0.0
List len 28: current_size 344 new_allocated 8 * 0.0
List len 29: current_size 344 new_allocated 8 * 0.0
List len 30: current_size 344 new_allocated 8 * 0.0
List len 31: current_size 344 new_allocated 8 * 0.0
List len 32: current_size 344 new_allocated 8 * 0.0
List len 33: current_size 344 new_allocated 8 * 0.0
List len 34: current_size 344 new_allocated 8 * 0.0
List len 35: current_size 344 new_allocated 8 * 0.0
List len 36: current_size 432 new_allocated 8 * 11.0
List len 37: current_size 432 new_allocated 8 * 0.0
List len 38: current_size 432 new_allocated 8 * 0.0
List len 39: current_size 432 new_allocated 8 * 0.0
List len 40: current_size 432 new_allocated 8 * 0.0
List len 41: current_size 432 new_allocated 8 * 0.0
List len 42: current_size 432 new_allocated 8 * 0.0
List len 43: current_size 432 new_allocated 8 * 0.0
List len 44: current_size 432 new_allocated 8 * 0.0
List len 45: current_size 432 new_allocated 8 * 0.0
List len 46: current_size 432 new_allocated 8 * 0.0
List len 47: current_size 528 new_allocated 8 * 12.0
List len 48: current_size 528 new_allocated 8 * 0.0
List len 49: current_size 528 new_allocated 8 * 0.0
List len 50: current_size 528 new_allocated 8 * 0.0
List len 51: current_size 528 new_allocated 8 * 0.0
List len 52: current_size 528 new_allocated 8 * 0.0
List len 53: current_size 528 new_allocated 8 * 0.0
List len 54: current_size 528 new_allocated 8 * 0.0
List len 55: current_size 528 new_allocated 8 * 0.0
List len 56: current_size 528 new_allocated 8 * 0.0
List len 57: current_size 528 new_allocated 8 * 0.0
List len 58: current_size 528 new_allocated 8 * 0.0
List len 59: current_size 640 new_allocated 8 * 14.0
List len 60: current_size 640 new_allocated 8 * 0.0
List len 61: current_size 640 new_allocated 8 * 0.0
List len 62: current_size 640 new_allocated 8 * 0.0
List len 63: current_size 640 new_allocated 8 * 0.0
List len 64: current_size 640 new_allocated 8 * 0.0
List len 65: current_size 640 new_allocated 8 * 0.0
List len 66: current_size 640 new_allocated 8 * 0.0
List len 67: current_size 640 new_allocated 8 * 0.0
List len 68: current_size 640 new_allocated 8 * 0.0
List len 69: current_size 640 new_allocated 8 * 0.0
List len 70: current_size 640 new_allocated 8 * 0.0
List len 71: current_size 640 new_allocated 8 * 0.0
List len 72: current_size 640 new_allocated 8 * 0.0
List len 73: current_size 768 new_allocated 8 * 16.0
List len 74: current_size 768 new_allocated 8 * 0.0
List len 75: current_size 768 new_allocated 8 * 0.0
List len 76: current_size 768 new_allocated 8 * 0.0
List len 77: current_size 768 new_allocated 8 * 0.0
List len 78: current_size 768 new_allocated 8 * 0.0
List len 79: current_size 768 new_allocated 8 * 0.0
List len 80: current_size 768 new_allocated 8 * 0.0
Through observation, it can be found that when the list increases from 0 to 80, the memory length of the new request is [4, 4, 8, 9, 10, 11, 12, 13, 14, 16]. Conversely, when remove or pop is executed to reduce the data in the list, the list will be automatically scaled down.
Expansion condition: the new length is greater than the length of the underlying array
Capacity reduction condition, the new length is less than half of the length of the underlying array.
Conclusion: avoid initializing lists using syntax similar to append, and give priority to list expressions
# Bad ❌ list_a = [] for i in range (50): list_a.append (I) # Good ✔️ list_b = [i for i in range (50)] conclusion:
① avoids using "+" to modify arrays
② tries to avoid using the append function multiple times.
At this point, the study of "the difference between extend (),"+" and "+ =" in Python is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.