lisa.utils.group_by_value#
- lisa.utils.group_by_value(mapping, key_sort=<function <lambda>>)[source]#
Group a mapping by its values
- Parameters:
mapping (collections.abc.Mapping or collections.abc.Sequence) – Mapping to reverse. If a sequence is passed, it is assumed to contain key/value subsequences.
key_sort (collections.abc.Callable) – The
key
parameter to asorted()
call on the mapping keys
- Return type:
The idea behind this method is to “reverse” a mapping, IOW to create a new mapping that has the passed mapping’s values as keys. Since different keys can point to the same value, the new values will be lists of old keys.
Example:
>>> group_by_value({0: 42, 1: 43, 2: 42}) OrderedDict([(42, [0, 2]), (43, [1])])