sequence_to_string¶
- lsst.utils.iteration.sequence_to_string(values: list[int | str]) str ¶
Convert a list of integers or strings into a compact string representation by merging consecutive values or sequences.
This function takes a list of integers or strings, sorts them, identifies sequences where consecutive numbers differ by a consistent stride, or strings with common prefixes, and returns a string that compactly represents these sequences. Consecutive numbers are merged into ranges, and strings with common prefixes are handled to produce a concise representation.
>>> getNameOfSet([1, 2, 3, 5, 7, 8, 9]) '1..3^5^7..9' >>> getNameOfSet(['node1', 'node2', 'node3']) 'node1..node3' >>> getNameOfSet([10, 20, 30, 40]) '10..40:10'
- Parameters:
- valueslist[int, str]:
A list of items to be compacted. Must all be of the same type.
- Returns:
- sequence_as_stringstr
A compact string representation of the input list.
Notes
The function handles both integers and strings.
- For strings with common prefixes, only the differing suffixes are
considered.
- The stride is determined as the minimum difference between
consecutive numbers.
Strings without common prefixes are listed individually.