Common data structures of python in LeetCode

Keywords: PHP Python Lambda Attribute

It is stated that the starting point of this article is not data structures, but objects that can be directly used in python for data structures.

1. Ordered sequence

1.1 immutable sequence

1.1.1 str

Built-in data structure, can be used directly without importing

Common Functions

format(*args, **kwargs)  # Various usage, the algorithm can be used in the string form of the binary conversion
split(sep=None, maxsplit=-1)
strip([chars])
join(iterable)
replace(old, new[, count])
count(sub[, start[, end]])
startswith(prefix[, start[, end]])
endswith(suffix[, start[, end]])

Common functions

  • Splicing (plus), s1 + s2
  • Slice, s[start: end: space]
  • Repeat (multiply), s * 10

1.1.2 tuple

Tuples, also known as static lists, are built-in data structures that can be used directly without importing.

  • Tuples are often used for multivariable assignment and multivalued return, but they are usually used without parentheses to make python look good.

  • It should be noted that when there is only one element in a tuple, it needs to be added after that element, such as t=(1,), otherwise it is not a tuple, but the type of the element.
  • It also supports stitching, slicing, repetition and other operations.
  • The only functions provided are index and count

1.2 Variable Sequence

1.2.1 list

Frequently used data structures can implement simple queues, stacks, etc.

Common Functions: Stitching, Repetition, Slicing

Powerful slicing function, can be used for value, replacement, deletion, etc.

  • lst[i:j] = t, where t is an iterative sequence
  • del lst[i:j], equivalent to lst[i:j]= [].

Common Functions

lst.sort(*, key=None, reverse=False)
lst.append(val)  # It can also be lst = lst + [val]
lst.clear()
lst.count(val)
lst.extend(t)  # or s += t  # += Actually, the _iadd_ method is called.
lst.pop(val=lst[-1])
lst.remove(val)
lst.reverse()
lst.insert(i, val)

1.2.2 deque

A bidirectional queue data structure of a linked list that inserts or deletes an element from the head or tail of the queue with a time complexity of O(1).
It can be used to represent FIFO.

Common Functions

from collections import deque
queue = deque([iterable[, maxlen]])
queue.append(val)  # Add an element to the right
queue.appendleft(val)  # Add an element to the left
queue.clear()  # ClearQueue
queue.count(val)  # Returns the number of occurrences of the specified element
queue.extend(iterable)  # Extending the elements of a sequence from the right side of the queue
queue.extendleft(iterable)  #  Extend the elements of a list from the left of the queue
queue.insert(val[, start[, stop]])  # Insert elements at specified locations
queue.pop()  # Get the rightmost element and delete it from the queue
queue.popleft()  # Get the leftmost element and delete it from the queue
queue.reverse()  # Queue reversal
queue.remove(val)  # Delete the specified element
queue.rotate(n=1)  # Put the right element on the left

1.2.3 bisect

A class of efficient half-search algorithms.
Using index to find an element on a list will take a linear proportion of the length of the list. The bisect_left functions provided by bisect use a half-split search algorithm, which can find a value in the sorted elements. The index returned by the bisect_left function represents the interpolation of the value to be searched in the sequence. Entry point.

import bisect
lst = list(range(10**6))
index1 = lst.index(654321)
index2 = bisect.bisect_left(lst, 987654)

Binary lookup is logarithmic in complexity. That is to say, it takes almost as long to search a list of 100,000 elements with bisect as it takes to search a list of 14 elements with index.

Common Functions

import bisect
bisect.bisect_left(a, x, lo=0, hi=len(a))
bisect.bisect_right(a, x, lo=0, hi=len(a))
bisect.bisect(a, x, lo=0, hi=len(a))
bisect.insort_left(a, x, lo=0, hi=len(a))
bisect.insort_right(a, x, lo=0, hi=len(a))
bisect.insort(a, x, lo=0, hi=len(a))

2. Unordered objects

2.1 Dictionary

General dictionaries are: disordered (there are also ordered dictionaries Ordered Dict), keys are immutable, and values are variable.

2.1.1 dict

Dictionaries, or maps, are stored in the form of key-value pairs. I don't think it's necessary to introduce them too much.

Common Functions

pop(key[, default])  # Delete key-value pairs by key (return default if no default is set)
setdefault(key[, default])  # Setting default values
update([other])  # Batch addition
get(key[, default])  # Get the value by the key (default value can be set if there is no key to prevent errors)
clear()  # Empty the dictionary
keys()  # Composing dictionary keys into new iterative objects
values()  # Composing values in dictionaries into new iterative objects
items()  # The key-value pairs of dictionaries are grouped into tuples to form new iterative objects.

2.1.2 defaultdict

If the key does not exist in dict, the error like KeyError will be reported. At this time, get method can be used to solve or catch exceptions. But this will feel troublesome, error-prone, and does not reflect python's concise style.

It's time to defaultdict. When you try to get a value that doesn't exist, you won't make a mistake.

Simple usage

from collections import defaultdict
d = defaultdict(lambda : value)
# When a non-existent key is taken, value is returned.

It inherits from dict, so it has the same method as dict.

2.1.3 Counter

Number of Statistics

The Counter class inherits dict, so it can use the methods in dict.

from collections import Counter
cnt = Counter([iterable-or-mapping])
cnt.elements()  # All elements
cnt.most_common([n])  # Specify a parameter n, list the first n elements, and list all without specifying parameters
cnt.subtract([iterable-or-mapping])  # Subtract the original element from the new element
cnt.update([iterable-or-mapping])  # Adding Elements

2.2 Sets

set, mainly used for de-duplication operations.

Notice that

  • Its definition can only be defined by examples, such as s= set();s={1,2,4,8}, but not by s={}. Because this definition is a dictionary, the method of collections cannot be used.
  • Unordered feature. Sometimes you will find that the same set is ordered when you output it N times, but you must also not believe that the set is ordered.

Common Functions

add(elem)  # Adding data to a collection
update(*others)  # Increasing iteratively
clear()  # Empty Set
discard(elem)  # Delete the value specified in the collection (if it does not exist, it will not be deleted)

3. heap queue

The data structure of priority queue can be realized.
The top n problem can be solved, such as finding the maximum or minimum number of 100 out of 100 million.

import heapq
nums = [randint(1, 1000) for x in range(100)]
print(heapq.nlargest(3, nums))
print(heapq.nsmallest(3, nums))

Common Functions

import heapq

heap = []  # Build heap
heapq.heappush(heap,item)  # Insert new values into the heap
item = heapq.heappop(heap)  # Minimum pop-up value
item = heap[0]  # View the minimum value in the heap, no pop-up
heapq.heapify(x)  # Converting a list to a heap in linear time
item = heapq.heapreplace(heap,item)  # Pop up a minimum value and insert item into the heap. The overall structure of the heap will not change.
heapq.heappoppush(heap, item)  # Pop up the minimum value and insert the new value into it.
heapq.merge(*iterables, key=None, reverse=False)  # Merge multiple heaps
heapq.nlargest(n, iterable, key=None)  # Find the maximum n numbers from the heap. The function of key is similar to that of key in sorted() method. It uses an attribute and function of a list element as the key.
heapq.nsmallest(n, iterable, key=None)  # Find the smallest n from the heap, as opposed to nlargest

4. Expansion

Because the number of leetcode problems brushed is still small, I don't know if the following classes will be used in the future, so I will list them first.

  • Array: An important difference from list is that array can only store the specified data type. When used, import array is required.
  • Queue: Queue from Queue import queue.
  • Ordered Dict: Ordered Dictionary
  • ChainMap: Connecting multiple dictionaries together
  • Binary correlation: bytes, bytearray, memoryview
  • nametuple: Named tuples that need to be imported
from collections import namedtuple

namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)

Posted by SidewinderX on Tue, 23 Jul 2019 05:40:28 -0700