Maps the elements of an iterable to categories using bisect
Project description
Maps the elements of an iterable to categories using bisect
pip install catmapper
Tested against Windows 10 / Python 3.10 / Anaconda
Args:
iterable (iterable): The iterable containing the elements to be categorized.
cats (list[list, list] | list[tuple | list]): The categories used for mapping. It can be provided in two formats:
- A list of two lists, where the first list represents the category labels and the second list represents
the corresponding values. The values don't have to be in ascending order.
- A list of pairs, where each pair consists of a category label and its corresponding value.
Returns:
list[tuple]: A list of tuples, where each tuple contains an element from the 'iterable' and its corresponding category.
Raises:
TypeError: If the 'cats' argument is not in the expected format or if the values are not in ascending order.
Examples:
import sys
from catmapper import category_mapping
# Example 1: Using a list with 2 lists
cats_ = [['barato', 'mais ou menos', 'caro', 'muito caro', 'absurdo'],
[1.3, 2, 3.1, 6.5, sys.maxsize]]
cervejas = [
("original", 2.5),
("Skol", 0.5),
("becks", 16),
("brahma", 1.4),
("heineken", 5.5),
]
print(category_mapping(cervejas, cats_))
# Output:
# [(('original', 2.5), 'caro'), (('Skol', 0.5), 'barato'), (('becks', 16), 'absurdo'),
# (('brahma', 1.4), 'mais ou menos'), (('heineken', 5.5), 'muito caro')]
# Example 2: Using a list of pairs
cats_ = [
("barato", 1.3),
("mais ou menos", 2),
("caro", 3.1),
("muito caro", 6.5),
("absurdo", sys.maxsize),
]
# also ok:
cats_ = [
("muito caro", 6.5),
("mais ou menos", 2),
("caro", 3.1),
("absurdo", sys.maxsize),
("barato", 1.3),
]
print(category_mapping(cervejas, cats_))
# Output:
# [(('original', 2.5), 'caro'), (('Skol', 0.5), 'barato'), (('becks', 16), 'absurdo'),
# (('brahma', 1.4), 'mais ou menos'), (('heineken', 5.5), 'muito caro')]
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
catmapper-0.10.tar.gz
(3.9 kB
view details)
Built Distribution
File details
Details for the file catmapper-0.10.tar.gz
.
File metadata
- Download URL: catmapper-0.10.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d56c9cfcdd28f7aa599fc7d2dc44bb1f605feb75b4afdfd630d014c836fa505f |
|
MD5 | f5a8627ec66125274e08aa8626056464 |
|
BLAKE2b-256 | 1bc7d69daf52df092edeaf5e2ed0294716557b42e94579db1046a4da26c07add |
File details
Details for the file catmapper-0.10-py3-none-any.whl
.
File metadata
- Download URL: catmapper-0.10-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1141b60c422f12f02949c2a4c70d86296452c218cfedadeac122cbb1770f51cc |
|
MD5 | a57c6015363aa95324080fba577a407d |
|
BLAKE2b-256 | 949bfdb7e3aaedfcce97687994436ada8a61b055e4f470beacaa11bf41dff939 |