Skip to main content

huffman

Generate Huffman codebooks! Huffman codes are the optimal way to compress individual symbols into a binary sequence that can be unambiguously decoded without inter-symbol separators (it is “prefix-free”).

Provided an iterable of 2-tuples in (symbol, weight) format, generate a Huffman codebook, returned as a dictionary in {symbol: code, ...} format.

>>> huffman.codebook([('A', 2), ('B', 4), ('C', 1), ('D', 1))
{'A': '10', 'B': '0', 'C': '110', 'D': '111'}

If you have an iterable of symbols, the collections.Counter is a handy way to tally them up.

>>> huffman.codebook(collections.Counter('man the stand banana man').items())
{' ': '111',
 'a': '10',
 'b': '0100',
 'd': '0110',
 'e': '11010',
 'h': '0101',
 'm': '1100',
 'n': '00',
 's': '11011',
 't': '0111'}

Metadata

Release files for huffman 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for huffman 0.1.2
File Interpreter ABI Platform
huffman-0.1.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Release files / huffman-0.1.2-py2.py3-none-any.whl

Download URL huffman-0.1.2-py2.py3-none-any.whl
Size 4.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
35234336f75b8ade2dbdd646c04d089ce79a0cc41724e02279103be864891a19
BLAKE2b-256 checksum
How to use checksums
c57299cf86630e26a02f2ee6ab649465fce7af7723635902cd79af91f6cc60e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page