Python does not have some useful or important data structures, like linked list, tree map, just like STL in C++.
This library aims to provide a python counterpart of C++ STL.
python3 -m pip install Python-Algorithm-pyal2. Examples github
Balanced search tree based map, TreeMap
import pyal
def main():
tree_map = pyal.TreeMap()
data = [(0, "a"), (1, "b"), (2, "c"), (3, "d"), (4, "e"), (5, "f")]
for key, value in data:
tree_map[key] = value
key = -1
value = tree_map.get(key)
if value is None:
print(f"key={key} does not exist")
key = 0
print(f"key={key}, value={tree_map[key]}")
key = 1
node = tree_map.lower_bound(key)
print(f"lower_bound({key=}): {node.get()=}")
node = tree_map.upper_bound(key)
print(f"upper_bound({key=}): {node.get()=}")
print(f"min key: {tree_map.key_list_begin().get()}")
print(f"max key: {tree_map.key_list_end().prev().get()}")Output
key=-1 does not exist
key=0, value=a
lower_bound(key=1): node.get()=1
upper_bound(key=1): node.get()=2
min key: 0
max key: 5
Please check github for all examples.
-
-
-
- search_KMP
- search_multipatterns
- todo
-
- Graph
- Dijkstra
- topological_traversal
-
is_none_or_emptyhistogram_asciiis_sorteduniquecmpsplit_data_by_funceqdiscrete_samplegroup_by_key_funtop_nclampargmaxargminmake_listswaprotatecopy_tokth_smallest_elementlower_boundupper_boundreverse_in_placesort_in_placefind_first_iffind_last_ifnext_permutationprev_permutationfactorialcombinatorial_numberpermutation_numbercombinations_with_duplicatelongest_common_substrtop_k_similar