ahvn.utils.basic.misc_utils module

ahvn.utils.basic.misc_utils.unique(iterable, key=lambda x: ...)[源代码]

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

参数:
  • 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.

返回:

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

返回类型:

List[Any]

示例

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

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

参数:

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

返回:

A flat list containing all elements from the nested iterable.

返回类型:

List[Any]

示例

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

Calculate specified percentiles from a frequency counter.

参数:
  • 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].

返回:

A dictionary mapping each requested percentile to its corresponding value.

返回类型:

Dict[int, Any]

示例

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