Skip to main content

lcs2

lcs2 is a Python package that helps find the longest common—or optimal weighted—subsequence of a pair of sequences and compute their diff. Among all optimal subsequences, it returns the one with the fewest runs (i.e., contiguous segments) across the two sequences.

Installation

pip install lcs2

Reference

The package provides the following functions, where weight[A, B] = Callable[[A, B], int | float].

If the weight function is provided, it should return positive values if and only if the two elements can be matched. If the weight function is omitted, the elements are considered equal if they are identical, and the classical LCS sequence is returned.

Function Signature Result
lcs Iterable[A], Iterable[B], weight[A, B]?, take_b? -> list[A] | list[B] Longest (or optimal weighted) common subsequence (LCS/OWCS)
lcs_indices Iterable[A], Iterable[B], weight[A, B]? -> list[tuple[int, int]] Indices of the LCS/OWCS
lcs_length Iterable[A], Iterable[B] -> int Length of the LCS
lcs_weight Iterable[A], Iterable[B], weight[A, B] -> int | float Weight of the OWCS
diff Iterable[A], Iterable[B], weight[A, B]? -> list[tuple[list[A], list[B]]] Differing segments of the sequences based on the LCS/OWCS
diff_ranges Iterable[A], Iterable[B], weight[A, B]? -> list[tuple[range, range]] Ranges of indices of the differing segments

Sample Usage

from lcs2 import diff, diff_ranges, lcs, lcs_indices, lcs_length, lcs_weight

a = 'Hello, world!'
b = 'Foobar'

print(lcs(a, b))  # ['o', 'o', 'r']
print(lcs_indices(a, b))  # [(4, 1), (8, 2), (9, 5)]
print(lcs_length(a, b))  # 3

print(diff(a, b))  # [(['H', 'e', 'l', 'l'], ['F']),
                   # ([',', ' ', 'w'], []),
                   # ([], ['b', 'a']),
                   # (['l', 'd', '!'], [])]
print(diff_ranges(a, b))  # [(range(0, 4), range(0, 1)),
                          # (range(5, 8), range(2, 2)),
                          # (range(9, 9), range(3, 5)),
                          # (range(10, 13), range(6, 6))]

print(lcs(
    'xo',
    'xXOo',
    weight=lambda ca, cb: 1 if ca.lower() == cb.lower() else 0,
    take_b=True,
))  # ['X', 'O']

print(lcs_weight(
    'xxxoo',
    'ooxxx',
    weight=lambda ca, cb: {'x': 1, 'o': 2}[ca] if ca == cb else 0,
))  # 4

Release files for lcs2 2.0.0

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

Source distribution (sdist)

Source distribution for lcs2 2.0.0
File Size Uploaded
lcs2-2.0.0.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lcs2 2.0.0
File Interpreter ABI Platform
lcs2-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / lcs2-2.0.0.tar.gz

Download URL lcs2-2.0.0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
eb6fb6531d12f84075d22c6af875f178c8d9e2984d9079234a50da5af0743722
BLAKE2b-256 checksum
How to use checksums
04d704c489df97ce02fa9b54bf6675d72169d11e14e758752fc0cef85bad4e90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.13

Release files / lcs2-2.0.0-py3-none-any.whl

Download URL lcs2-2.0.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf016b131c37bf058d967fb6aba8c9c5f17e7911793f043eab4f0956ae829d26
BLAKE2b-256 checksum
How to use checksums
c1c92a7f2cf173c25ed9ebe6784ea88cd67cb935f7ba6e1b4ae8dc7545490122
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.13

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

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