ReversibleDict
*~~~ A dictionary being able to perform a reverse lookup ~~~*
How does it work?
I calculate a hash of the value (or a hash of the value's __str__ representation for unhashable types - i.e. lists) and internally store the reverse mapping, so no linear search through the values if you thought about that.
What if there is more than 1 value for a key?
I simply return a list of all keys that matched.
Usage
>>> from reversibledict import ReversibleDict
>>> r = ReversibleDict()
>>> r['a'] = 1
>>> r['b'] = 2
>>> r['c'] = 1
>>> r
{'a': 1, 'c': 1, 'b': 2}
>>> r.key_for_value(2)
'b'
>>> r.key_for_value(1)
['a', 'c']
>>> r.key_for_value(666) == None
True
It can also deal with unhashable types such as lists:
>>> from reversibledict import ReversibleDict
>>> r = ReversibleDict()
>>> r['a'] = [1,2,3]
>>> r['b'] = [3,2,1]
>>> r['c'] = [1]
>>> r['d'] = [1,2,3]
>>> r
{'a': [1, 2, 3], 'c': [1], 'b': [3, 2, 1], 'd': [1, 2, 3]}
>>> r.key_for_value([1,2,3])
['a', 'd']
>>> r.key_for_value([3,2,1])
'b'
>>> r.key_for_value([]) == None
True
If you don't like the inconsistency that it is returning a list if there is more than 1 matching value, None if there is no matching value and a scalar if there is exactly 1 matching value then you can do the following:
>>> from reversibledict import ReversibleDict
>>> r = ReversibleDict(reverse_as_list=True)
>>> r['a'] = 1
>>> r['b'] = 2
>>> r['c'] = 1
>>> r
{'a': 1, 'c': 1, 'b': 2}
>>> r.key_for_value(2)
['b']
>>> r.key_for_value(1)
['a', 'c']
>>> r.key_for_value(666)
[]
Metadata
Release files for reversibledict 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| reversibledict-0.2.3.tar.gz | 4.3 kB | Without provenance | Details for reversibledict-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| reversibledict-0.2.3-py3-none-any.whl | Python 3 | none | any | Details for reversibledict-0.2.3-py3-none-any.whl |
Total release size: 8.0 kB
Release files / reversibledict-0.2.3.tar.gz
| Download URL | reversibledict-0.2.3.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
088918e1c011c643bac9a3739b5198bfa835adfe25e84e44f13d172825a1a8cb
|
|
BLAKE2b-256 checksum How to use checksums |
1da42714b301a7a403349ee45abe93ef73e3502110928f6f217671084430e73d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Release files / reversibledict-0.2.3-py3-none-any.whl
| Download URL | reversibledict-0.2.3-py3-none-any.whl |
|---|---|
| Size | 3.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
506b450b0bde6b2ed1ad4903278fb14be032adc93208c95ca035c149998ffe3d
|
|
BLAKE2b-256 checksum How to use checksums |
b034fab7c0193e43a36a8f83ce82d0f7371f2cf19249651ec032d7e6fd4870af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Security for reversibledict 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.