The principle underlying python list

Python list data structure is kind of how?

He says the book is: The list can be achieved arrays and linked lists.
Order table is how is it? General sequence table is an array.

A linear list is a collection, which allows the user to insert at any position, delete, access and replace elements.
List implementation is based on an array or list-based structure. When using a list iterator, double-linked list structure faster than singly-linked list structure.
Ordered list of elements is always in accordance with the elements arranged in ascending or descending.


Implementation details
list in python English name list, making it easy and other languages (C ++, Java, etc.) common to confuse list standard library. In fact CPython list is not a list of (possible into English easier to understand: python in the list is not a list). In CPython, the list is implemented as a variable length array.

Refer to "Python Advanced Programming (2nd Edition)"

From the detail view, Python list is continuous array of references to other objects thereof. And the pointer to the length of the array is stored in a list head structure. This means that every time you add or delete an element, consisting of an array of references to the need for standard size (reallocation). Fortunately, Python uses the exponential distribution when creating these arrays, so operations are not always necessary to change the size of the array. However, because of this reason add or remove elements amortized less complex.

Unfortunately, in the normal chain calculation complexity in Python "small price" some other operation is relatively high.

Using list.insert (i, item) the method to insert an element at an arbitrary position - complexity of O (N)
using list.pop (i) or list.remove (value) removes an element - complexity of O (N)


Algorithm efficiency list of
time complexity can be used to measure:

index() O(1)
append O(1)
pop() O(1)
pop(i) O(n)
insert(i,item) O(n)
del operator O(n)
iteration O(n)
contains(in) O(n)
get slice[x:y] O(k)
del slice O(n)
set slice O(n+k)
reverse O(n)
concatenate O(k)
sort O(nlogn)
multiply O(nk)

The lower value represents O greater efficiency of brackets


Lists and tuples
difference between lists and tuples are obvious:
the list is dynamic, which may be of the standard size (re-allocation);
and tuples are immutable once created can not be changed.

c tuple in the list and is very similar to realize, for a large number of elements of the time,
is an array pointer to the corresponding object, faster than the tuple list reasons not found.
But for small objects is, there will be a tuple object pool so small, repetitive use tuple also benefit.

Why should tuple, there are a lot of rationality.
The actual situation indeed there are many fixed-size list structure, such as a two-dimensional geographic coordinates;
Further tuple element naturally also imparted to the read-only attribute.

Think tuple faster than the list of people is probably the python tuple and list of analogy into an array and a list in C ++.


Related documents
Dive Into Python lists internal implementation: HTTP: //python.jobbole.com/82549/
[Python] underlying details of the list, tuple, dictionary, set the: https: //blog.csdn.net/siyue0211/article/details / 80,560,783
Python list: beginners should know how to operate and the internal implementation: HTTPS: //mp.weixin.qq.com/s/IkFak4iYYqW7u61P7eu22g
Python study notes - list the internal implementation: https: //www.jianshu.com/p/cd75475168ae
Python be appreciated from the underlying execution: https: //www.csdn.net/article/2015-05-28/2824795

Guess you like

Origin www.cnblogs.com/liujiliang/p/11390322.html