group_by
- mdtools.numpy_helper_functions.group_by(keys, values, assume_sorted=False, return_keys=False, debug=False)[source]
Group the elements of values by their keys.
- Parameters:
keys (
array_like
) – 1-dimensional array of keys used to group the elements of values.values (
array_like
) – The values (elements) to be grouped by keys. values must be convertible to anumpy.ndarray
and it must have the same length as keys.assume_sorted (
bool
, optional) – IfTrue
, the key-value pairs are assumed to be sorted with respect to the keys, which can speed up the calculation. IfFalse
, keys and values are sorted by keys usingnumpy.argsort()
withkind='stable'
.return_keys (
bool
, optional) – IfTrue
, return the unique, sorted keys.debug (
bool
, optional) – IfTrue
, check the input arguments.Deprecated since version 0.0.6.0: This argument is without functionality and will be removed in a future release.
- Returns:
unique_keys (
numpy.ndarray
) – The unique, sorted keys. Only returned, if return_keys isTrue
.grouped_values (
list
ofnumpy.ndarrays
) – List of numpy.ndarrays, one array for all elements of values belonging to the same key. The arrays in the list are sorted according to the unique, sorted keys.
Examples
>>> keys = ["a", "b", "c", "c", "b", "c"] >>> values = [1, 2, 3, 3, 2, 3] >>> k, v = mdt.nph.group_by(keys, values, return_keys=True) >>> k array(['a', 'b', 'c'], dtype='<U1') >>> v [array([1]), array([2, 2]), array([3, 3, 3])] >>> {k: v for k, v in zip(k, v)} {'a': array([1]), 'b': array([2, 2]), 'c': array([3, 3, 3])}
>>> keys = [2, 1, 2] >>> values = [np.array([0, 0]), np.array([1, 1]), np.array([2, 2])] >>> k, v = mdt.nph.group_by(keys, values, return_keys=True) >>> k array([1, 2]) >>> v [array([[1, 1]]), array([[0, 0], [2, 2]])] >>> {k: v for k, v in zip(k, v)} {1: array([[1, 1]]), 2: array([[0, 0], [2, 2]])}