Get the App
SLTechnology News&Howtos  ›  Development  › 

Analysis of the function of python SortedList

Shulou Source: shulou.com Published: 2022-06-03 18:58:32 10月05日 Update

This article mainly explains "Python SortedList role analysis," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "Python SortedList role analysis" bar!

directory

SortedList

method

1. add values

2. Remove value

3. find

4. iteration value

5. other

SortedList ordered sequence class sortedcontainers.SortedList(iterable=None, key=None) Method 1. Add Value

SortedList.add(value) adds new elements and sorts them. Time complexity O(log(n)).

SortedList.update(iterable) Sorts all iterable elements added. Time complexity O(k*log(n)).

2. Remove value

SortedList.clear() removes all elements. Time complexity: O(n).

SortedList.discard(value) removes a value element, no error if the element does not exist. Time complexity O(log(n)).

SortedList.remove(value) removes a value element, if the element does not exist, an error ValueError is reported. Time complexity O(log(n)).

SortedList.pop(index=-1) removes a specified subscript element, if the ordered sequence is empty or subscript exceeds, an IndexError is reported. Time complexity: O(log)

3. find

SortedList.bisect_left(value) Finds the index where an element can be inserted, or if this value already exists, inserts it before all existing values (left). Time complexity O(log(n)).

s = SortedList([1,2,3,9,8,6,5,5,5,5,5])s.bisect_left(5)Out[5]: 3sOut[6]: SortedList([1, 2, 3, 5, 5, 5, 5, 5, 6, 8, 9])

SortedList.bisect_right(value) Finds the index where an element can be inserted, or if this value already exists, inserts it after all values that already exist (right). Time complexity O(log(n)).

s.bisect_right(5)Out[7]: 8sOut[8]: SortedList([1, 2, 3, 5, 5, 5, 5, 5, 6, 8, 9])

SortedList.count(value) Finds the number of occurrences of an element. Time complexity O(log(n)).

s.count(5)Out[9]: 5

SortedList.index(value, start=None, Stop=None) Find the index where value appears for the first time in the index range [start,stop]. If value does not exist, an error ValueError is reported. Time complexity O(log(n)).

4. iteration value

SortedList.irange(minimum =None, maximum=None, inclusive=True, True, reverse=False) Returns an iterable value between value=[minimum, maximum], inclusive = Ture, True The first True indicates that the index minimum is included, the second Ture indicates that the index maximum is included, reverse indicates whether the iterable value returned is reversed.

SortedList. isslice (start=None, stop=None, reverse=False) returns iterable values (slices) between index=[start, stop).

5. other

SortedList.copy() returns a shallow copy ordered sequence. Time complexity: O(n)

Shallow copy (1) direct assignment, default shallow copy pass object reference only, original list changes, assigned list will do the same change.

a = [1,2,3]b=abOut[60]: [1, 2, 3]a[0]=0aOut[62]: [0, 2, 3]bOut[63]: [0, 2, 3]

Shallow copy (2)copy function, shallow copy passes a reference to the child object, the original data changes, only the child object will change.

a = [[1],2,3]b = a.copy()aOut[85]: [[1], 2, 3]bOut[86]: [[1], 2, 3]#Object does not change a.append(4)aOut[88]: [[1], 2, 3, 4]bOut[89]: [[1], 2, 3]#Child object changes a[0].append(2)aOut[91]:[[1, 2], 2, 3, 4]bOut[92]: [[1, 2], 2, 3]

At this point, I believe that everyone has a deeper understanding of the "Python SortedList role analysis", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Complexity element complexity time iteration object copy ordering subscript index function analysis sequence method primitive inter location content learning sorting Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Linux Redmi Xiaomi