Pure python implementation of a sparse array.
Project description
This code has been transpiled from js-sparse-array and has minimal changes.
Usage
SparseArray implements MutableSequence.
We suggest you import the SparseArray as follows:
>>> from bitmap_sparse_array import SparseArray
Setting and getting
>>> sa = SparseArray() >>> sa[1000000] = 'value' >>> sa[1000000] 'value' >>> sa[1] IndexError() >>> len(sa) 1000001
Iterating
Due to the sparse nature of SparseArray, it does not make sense to iterate over all values in the traditional sense because of possible omitted indices. Therefore, you cannot iterate over the SparseArray itself. Instead, 3 generators are provided.
>>> sa = SparseArray() >>> sa[0] = '0' >>> sa[2] = '2'
SparseArray.indices() returns a generator of indices that have values.
>>> list(sa.indices()) [0, 2]
SparseArray.items() returns a generator of tuples of indices and values, similar to Mapping.items().
>>> list(sa.items()) [(0, '0'), (2, '2')]
SparseArray.values(*, default=None, start=0, end=None) returns a generator of values with a default value for non-set indices.
>>> list(sa.values()) ['0', None, '2'] >>> list(sa.values(default='default')) ['0', 'default', '2']
For the same reasons, you cannot get or set slices of a SparseArray, nor insert()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bitmap_sparse_array-0.1.3.tar.gz
.
File metadata
- Download URL: bitmap_sparse_array-0.1.3.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb4c94bd204bbef7dc417f4ed2aae2b3437fc75278e1b547619f2c5e63de996f |
|
MD5 | 6c438ca3ca5914be93b12a8b932d6725 |
|
BLAKE2b-256 | 804cd0ae233e1112dc983cb34d3a0944a418cdaa04c65f5cd98af602823c6bd7 |
File details
Details for the file bitmap_sparse_array-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: bitmap_sparse_array-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 646c566d551bac22b70f242ca167fe812db76805335adf6609f17a6fe6fcb35a |
|
MD5 | 931782a721c3af0501c11d0cdbf4e130 |
|
BLAKE2b-256 | dfed16911a2647053474d1dfb409d65bde606b30d8f2274243708fe2dafe6a49 |