Skip to main content

A sorted list for Python.

Project description

A sorted list for Python

Have you ever needed to keep a bunch of items in a specified order? For example file system items are usually shown in alphabetical order or to show events chronologically. In all such circumstances a sorted list would ease coding.

>>> from sorted_list import SortedList
>>> sl = SortedList()
>>> sl.add(3)
0
>>> print(sl)
[3]
>>> sl.add(5)
1
>>> print(sl)
[3, 5]
>>> sl.add(2)
0
>>> print(sl)
[2, 3, 5]

SortedList

SortedList is a sequence-like, iterable, and iterator class. This class is not thread safe.

Collisions

When you want to add a value to the sorted list and there is already the same value, or a value which is evaluated to be the same, this situation is called collision. This module offers three ways to deal with the collisions and aggregate them into the CollisionPolicy enumeration:

  1. IGNORE: The value will be ignored and will not be inserted into the list.
  2. END: The value will be inserted at the end of all the same values.
  3. START: The value will be inserted at the end of all the same values.

The initializer

sorted_list.SortedList.__init__(self, *, cp: CollisionPolicy = CollisionPolicy.IGNORE, key: Callable[[Any], Any] = None):

This is the initializer of the class with two keyword-only arguments. The 'cp' parameter accepts a CollisionPolicy which defaults to IGNORE. It can be set at any time with 'cp' property. The 'key' callable acts like key parameters in Python ecosystem. The callable must accepts a value and returns a result as well and if it does not provide values will be compared directly without any intermediate comparers. This attribute can not be changed. To change this attribute, you must instantiate a new sorted list.

Properties

sorted_list.SortedList.items:

Gets or sets the items of this object. It returns a copy of the objects in the list as a regular list or sets the underlying list with the the sorted version of argument provided.

sorted_list.SortedList.cp:

Gets or sets the collision policy for this object at any time.

Methods

sorted_list.SortedList.count(__value: Any, /) -> int:

Returns number of occurrences of '__value' in the SortedList.

sorted_list.SortedList.index(value: Any, start: int = 0, end: int | None = None) -> tuple[bool, int | slice]:

This method is the backbone of this class. It returns the index of 'value' in the SortedList. You can specifies an interval in the form of [start, end) to search for index, 'start' is included and 'end' in excluded. 'start' and 'end' are defaulted to 0 and None respectively which means 0 <= index < len(internal list). If you specify each boundary beyond these boundaries, they automatically will be clipped to these deafults. If there are the same value at the boundary of the specified interval, this method returns an index or a slice object that might lie outside of the interval.

It returns a pair, the first element specifies whether value existed in the list (True if existed). If existed (True), the second value will be an integer or a slice object. If there is one such value, the second element will be its index in the list, or if there are multiple such values the second element will be a slice object. If not existed (False) the second element will be the proper insertion position of the value in the list.

Exceptions:

  • TypeError: at least one of the 'start' or 'end' parameters got improper value
  • ValueError: 'start' was evaluated to be greater than 'end' or 'value' is not compatible with 'key' comparer
  • IntervalError: it is impossible to put 'value' in this interval [start, end)

sorted_list.SortedList.add(value: Any, start: int = 0, end: int | None = None, cp: CollisionPolicy | None = None) -> None | int:

Adds 'value' into its correct position in the SortedList. It accepts a 'cp' parameter which can be any CollisionPolicy value and defaults to None. None means use object default CollisionPolicy or you can specifies the policy for this add operation. This method returns the insertion position as an integer or None if IGNORE CollisionPolicy prevented the insertion. You can specify an interval like 'index' method.

sorted_list.SortedList.merge(items: Iterable[Any], cp: CollisionPolicy | None = None) -> None

Merges an interable of items into their suitable positions in the list. You can specify a custom collision policy for this operation.

Operators

len

It is possible to get the length of the underlying list with the builtin len function.

>>> from sorted_list import SortedList
>>> sl = SortedList()
>>> len(sl)
0
>>> sl.items = [5, 0, 2]
>>> len(sl)
3
>>> print(sl)
[0, 2, 5]

Subscript notation

You can get a value at the specified index by subscript notation:

>>> sl = SortedList()
>>> sl.merge([3, 5, 0, 0])
>>> print(sl)
[0, 3, 5]
>>> sl[0]
0
>>> sl[1]
3
>>> sl[2]
5
>>> sl[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Programming\Exercises\Python\sorted-list\sorted_list.py", line 136, in __getitem__
    return self._items[__idx]
IndexError: list index out of range

del

Deletes the specified list position (index).

Arithmetic comparisons

Instances of this class can be compared with any regular lists with arithmetic comparison operators of <, >, <=, =>. ==, and !=.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sorted_list-1.0.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sorted_list-1.0.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file sorted_list-1.0.0.tar.gz.

File metadata

  • Download URL: sorted_list-1.0.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sorted_list-1.0.0.tar.gz
Algorithm Hash digest
SHA256 54e08eb03f52e01b4ae38ff806b295e4a28fe52dec020c993bdd5748a6a71a41
MD5 8ea9ea61842606fe9172aac08c5c933b
BLAKE2b-256 a85ba21d0c850d930a12c7e9d41dd09937d34a1b455569ba7544dd15b465e252

See more details on using hashes here.

File details

Details for the file sorted_list-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sorted_list-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sorted_list-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70179caa8e2b073aa6c1855897d230d75ad4342ef71b474b6571f50d4456ef97
MD5 2ed505bb2175eb150e8abb6c9b743022
BLAKE2b-256 c0bd1b1005d7188bc10de4081ce8a7a1b363ec586363581a71d9eb7116e31a77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page