sorting#
- irmsd.sorting.first_by_assignment(items: Sequence[T], assignments: Sequence[SupportsInt]) list[T][source]#
Select the first representative of each assignment label.
- Parameters:
items – Sequence of arbitrary objects (e.g. ASE Atoms, numpy arrays, ints, …).
assignments – Sequence of integer-like labels (same length as items). The i-th entry is the assignment for items[i].
- Returns:
A new list containing the first occurrence of each assignment, in the order the labels first appear.
- Return type:
list[T]
Example
>>> items = list("abcdefgh") >>> assignments = [1, 1, 1, 2, 3, 3, 3, 3] >>> first_by_assignment(items, assignments) ['a', 'd', 'e']
- irmsd.sorting.group_by(items: Iterable[T], key: Callable[[T], K]) Dict[K, List[T]][source]#
Group arbitrary items according to a key function.
This is a fully general utility: it works for integers, strings, objects, or domain-specific types such as ASE Atoms.
- Parameters:
items (
iterableofT) – The objects to be grouped.key (
callable) – Function mapping an item -> hashable key (str, int, tuple, …).
- Returns:
groups – Mapping key -> list of items having that key.
- Return type:
Examples
1. Grouping ASE Atoms by chemical formula
>>> from ase.build import molecule >>> atoms_list = [molecule("H2O"), molecule("CH4"), molecule("H2O")] >>> groups = group_by(atoms_list, key=lambda a: a.get_chemical_formula()) >>> groups.keys() dict_keys(['H2O', 'CH4'])
2. Grouping integers by parity
>>> nums = [1, 2, 3, 4, 5] >>> group_by(nums, key=lambda x: x % 2) {1: [1, 3, 5], 0: [2, 4]}
3. Grouping strings by first character
>>> words = ["apple", "ape", "bat", "boat"] >>> group_by(words, key=lambda w: w[0]) {'a': ['apple', 'ape'], 'b': ['bat', 'boat']}
- irmsd.sorting.sort_by_value(items: Sequence[T], values: Sequence[V], *, descending: bool = False) tuple[list[T], list[V]][source]#
Sort items according to the corresponding numeric values.
- Parameters:
items – Sequence of arbitrary objects (ASE Atoms, numpy arrays, ints, …).
values – Sequence of numeric values (same length as items). Must contain int-, float-, or numpy-number-like entries.
descending – If True, sort from largest to smallest.
- Returns:
(sorted_items, sorted_values) – The items and their values sorted together.
- Return type:
(list[T],list[V])
Example
>>> items = ["a", "b", "c"] >>> values = [3.0, 1.0, 2.0] >>> sort_by_value(items, values) (['b', 'c', 'a'], [1.0, 2.0, 3.0])
>>> sort_by_value(items, values, descending=True) (['a', 'c', 'b'], [3.0, 2.0, 1.0])
Modules