ahvn.utils.basic.misc_utils module¶

ahvn.utils.basic.misc_utils.unique(iterable, key=lambda x: ...)[source]¶

Return a list of unique elements from an iterable, preserving order.

Parameters:
  • iterable (Iterable[Any]) – The input iterable to filter for unique elements.

  • key (callable, optional) – A function to extract a comparison key from each element. Defaults to the identity function.

Returns:

A list containing only the unique elements from the input iterable.

Return type:

List[Any]

Examples

>>> unique([1, 2, 2, 3, 1])
[1, 2, 3]
>>> unique(['apple', 'banana', 'orange'], key=len)
['apple', 'banana']
ahvn.utils.basic.misc_utils.lflat(iterable)[source]¶

Flatten a nested iterable (2 levels deep) into a single list.

Parameters:

iterable (Iterable[Iterable[Any]]) – The nested iterable to flatten.

Returns:

A flat list containing all elements from the nested iterable.

Return type:

List[Any]

Examples

>>> lflat([[1, 2], [3, 4]])
[1, 2, 3, 4]
ahvn.utils.basic.misc_utils.counter_percentiles(counter, percentiles={0, 25, 50, 75, 100})[source]¶

Calculate specified percentiles from a frequency counter.

Parameters:
  • counter (Dict) – A dictionary where keys are values and values are their frequencies.

  • percentiles (List[Union[int, float]]) – List of percentiles to calculate. Defaults to [0, 25, 50, 75, 100].

Returns:

A dictionary mapping each requested percentile to its corresponding value.

Return type:

Dict[int, Any]

Examples

>>> counter = {1: 2, 2: 3, 3: 5}
>>> counter_percentiles(counter, [0, 50, 100])
{0: 1, 50: 2, 100: 3}