torchoutil.pyoutil.collections module

all_eq(
it: Iterable[T],
eq_fn: Callable[[T, T], bool] | None = None,
) bool[source]

Returns true if all elements in iterable are equal.

Note: This function returns True for iterable that contains 0 or 1 element.

all_ne(
it: Iterable[T],
ne_fn: Callable[[T, T], bool] | None = None,
use_set: bool = False,
) bool[source]

Returns true if all elements in iterable are differents.

Note: This function returns True for iterable that contains 0 or 1 element.

argmax(
x: Iterable,
) int[source]
argmin(
x: Iterable,
) int[source]
contained(x: ~torchoutil.pyoutil.collections.T, include: ~typing.Iterable[~torchoutil.pyoutil.collections.T] | None = None, exclude: ~typing.Iterable[~torchoutil.pyoutil.collections.T] | None = None, *, match_fn: ~typing.Callable[[~torchoutil.pyoutil.collections.T, ~torchoutil.pyoutil.collections.T], bool] = <built-in function eq>, order: ~typing.Literal['left', 'right'] = 'right') bool[source]

Returns True if name in include set and not in exclude set.

dict_list_to_list_dict(
dic: Mapping[T, Iterable[U]],
key_mode: Literal['same', 'intersect'],
default_val: Any = None,
) List[Dict[T, U]][source]
dict_list_to_list_dict(
dic: Mapping[T, Iterable[U]],
key_mode: Literal['union'] = 'union',
default_val: W = None,
) List[Dict[T, U | W]]

Convert dict of lists with same sizes to list of dicts.

Example 1

` >>> dic = {"a": [1, 2], "b": [3, 4]} >>> dict_list_to_list_dict(dic) ... [{"a": 1, "b": 3}, {"a": 2, "b": 4}] `

Example 2

` >>> dic = {"a": [1, 2, 3], "b": [4], "c": [5, 6]} >>> dict_list_to_list_dict(dic, key_mode="union", default=-1) ... [{"a": 1, "b": 4, "c": 5}, {"a": 2, "b": -1, "c": 6}, {"a": 3, "b": -1, "c": -1}] `

dump_dict(
dic: Mapping[str, T] | None = None,
/,
join: str = ', ',
fmt: str = '{key}={value}',
ignore_lst: Iterable[T] = (),
**kwargs,
) str[source]

Dump dictionary of scalars to string function to customize representation.

Example 1:

` >>> d = {"a": 1, "b": 2} >>> dump_dict(d) ... 'a=1, b=2' `

filter_iterable(it: ~typing.Iterable[~torchoutil.pyoutil.collections.T], include: ~typing.Iterable[~torchoutil.pyoutil.collections.T] | None = None, exclude: ~typing.Iterable[~torchoutil.pyoutil.collections.T] | None = None, *, match_fn: ~typing.Callable[[~torchoutil.pyoutil.collections.T, ~torchoutil.pyoutil.collections.T], bool] = <built-in function eq>, order: ~typing.Literal['left', 'right'] = 'right') List[T][source]
find(
target: T,
it: Iterable[V],
*,
match_fn: Callable[[V, T], bool] = operator.eq,
order: Literal['right'] = 'right',
default: U = -1,
return_value: Literal[False] = False,
) int | U[source]
find(
target: T,
it: Iterable[V],
*,
match_fn: Callable[[T, V], bool] = operator.eq,
order: Literal['left'],
default: U = -1,
return_value: Literal[False] = False,
) int | U
find(
target: T,
it: Iterable[V],
*,
match_fn: Callable[[V, T], bool] = operator.eq,
order: Literal['right'] = 'right',
default: U = -1,
return_value: Literal[True],
) Tuple[int | U, V | U]
find(
target: T,
it: Iterable[V],
*,
match_fn: Callable[[T, V], bool] = operator.eq,
order: Literal['left'],
default: U = -1,
return_value: Literal[True],
) Tuple[int | U, V | U]
flat_dict_of_dict(
nested_dic: Mapping[str, Any],
*,
sep: str = '.',
flat_iterables: bool = False,
overwrite: bool = True,
) Dict[str, Any][source]

Flat a nested dictionary.

Example 1

` >>> dic = { ...     "a": 1, ...     "b": { ...         "a": 2, ...         "b": 10, ...     }, ... } >>> flat_dict_of_dict(dic) ... {"a": 1, "b.a": 2, "b.b": 10} `

Example 2

` >>> dic = {"a": ["hello", "world"], "b": 3} >>> flat_dict_of_dict(dic, flat_iterables=True) ... {"a.0": "hello", "a.1": "world", "b": 3} `

Args:

nested_dic: Nested mapping containing sub-mappings or iterables. sep: Separators between keys. flat_iterables: If True, flat iterable and use index as key. overwrite: If True, overwrite duplicated keys in output. Otherwise duplicated keys will raises a ValueError.

flat_list_of_list(
lst: Iterable[Sequence[T]],
return_sizes: Literal[True] = True,
) Tuple[List[T], List[int]][source]
flat_list_of_list(
lst: Iterable[Sequence[T]],
return_sizes: Literal[False],
) List[T]

Return a flat version of the input list of sublists with each sublist size.

flatten(
x: T_BuiltinScalar,
start_dim: int = 0,
end_dim: int | None = None,
) List[T_BuiltinScalar][source]
flatten(
x: Iterable[T_BuiltinScalar],
start_dim: int = 0,
end_dim: int | None = None,
) List[T_BuiltinScalar]
flatten(
x: Any,
start_dim: int = 0,
end_dim: int | None = None,
is_scalar_fn: Callable[[Any], typing_extensions.TypeGuard[T]] | Callable[[Any], typing_extensions.TypeIs[T]] = is_builtin_scalar,
) List[Any]
intersect(
*args,
start=None,
)[source]
intersect_lists(
lst_of_lst: Sequence[Iterable[T]],
) List[T][source]

Performs intersection of elements in lists (like set intersection), but keep their original order.

is_full(
it: Iterable[T],
eq_fn: Callable[[T, T], bool] | None = None,
) bool[source]

Returns true if all elements in iterable are equal.

Note: This function returns True for iterable that contains 0 or 1 element.

is_sorted(
x: Iterable[Any],
*,
reverse: bool = False,
strict: bool = False,
) bool[source]
is_unique(
it: Iterable[T],
ne_fn: Callable[[T, T], bool] | None = None,
use_set: bool = False,
) bool[source]

Returns true if all elements in iterable are differents.

Note: This function returns True for iterable that contains 0 or 1 element.

list_dict_to_dict_list(
lst: Iterable[Mapping[K, V]],
key_mode: Literal['intersect', 'same'] = 'same',
default_val: Any = None,
*,
default_val_fn: Any = None,
list_fn: None = None,
) Dict[K, List[V]][source]
list_dict_to_dict_list(
lst: Iterable[Mapping[K, V]],
key_mode: Literal['union'],
default_val: Any = None,
*,
default_val_fn: Callable[[K], X],
list_fn: None = None,
) Dict[K, List[V | X]]
list_dict_to_dict_list(
lst: Iterable[Mapping[K, V]],
key_mode: Literal['union'],
default_val: W = None,
*,
default_val_fn: None = None,
list_fn: None = None,
) Dict[K, List[V | W]]
list_dict_to_dict_list(
lst: Iterable[Mapping[K, V]],
key_mode: Literal['intersect', 'same', 'union'] | Iterable[K] = 'same',
default_val: W = None,
*,
default_val_fn: Callable[[K], X] | None = None,
list_fn: Callable[[List[V | W | X]], Y],
) Dict[K, Y]

Convert list of dicts to dict of lists.

Args:

lst: The list of dict to merge. key_mode: Can be “same” or “intersect”.

If “same”, all the dictionaries must contains the same keys otherwise a ValueError will be raised. If “intersect”, only the intersection of all keys will be used in output. If “union”, the output dict will contains the union of all keys, and the missing value will use the argument default_val.

default_val: Default value of an element when key_mode is “union”. defaults to None. default_val_fn: Function to return the default value according to a specific key. defaults to None. list_fn: Optional function to build the values. defaults to identity.

prod(
args: Iterable[T_SupportsMul],
/,
*,
start: T_SupportsMul = 1,
) T_SupportsMul[source]
prod(
*args: T_SupportsMul,
start: T_SupportsMul = 1,
) T_SupportsMul
prod(
arg0: T_SupportsMul,
/,
*args: T_SupportsMul,
start: T_SupportsMul | None = 1,
) T_SupportsMul
recursive_generator(
x: Any,
) Generator[Tuple[Any, int, int], None, None][source]
reduce_add(
args: Iterable[T_SupportsAdd],
/,
*,
start: T_SupportsAdd,
) T_SupportsAdd[source]
reduce_add(
*args: T_SupportsAdd,
start: T_SupportsAdd,
) T_SupportsAdd
reduce_add(
arg0: T_SupportsAdd,
/,
*args: T_SupportsAdd,
start: T_SupportsAdd | None = None,
) T_SupportsAdd
reduce_and(
args: Iterable[T_SupportsAnd],
/,
*,
start: T_SupportsAnd,
) T_SupportsAnd[source]
reduce_and(
*args: T_SupportsAnd,
start: T_SupportsAnd,
) T_SupportsAnd
reduce_and(
arg0: T_SupportsAnd,
/,
*args: T_SupportsAnd,
start: T_SupportsAnd | None = None,
) T_SupportsAnd
reduce_mul(
args: Iterable[T_SupportsMul],
/,
*,
start: T_SupportsMul,
) T_SupportsMul[source]
reduce_mul(
*args: T_SupportsMul,
start: T_SupportsMul,
) T_SupportsMul
reduce_mul(
arg0: T_SupportsMul,
/,
*args: T_SupportsMul,
start: T_SupportsMul | None = None,
) T_SupportsMul
reduce_or(
args: Iterable[T_SupportsOr],
/,
*,
start: T_SupportsOr,
) T_SupportsOr[source]
reduce_or(
*args: T_SupportsOr,
start: T_SupportsOr,
) T_SupportsOr
reduce_or(
arg0: T_SupportsOr,
/,
*args: T_SupportsOr,
start: T_SupportsOr | None = None,
) T_SupportsOr
shuffled(
x: MutableSequence[T],
*,
seed: int | None = None,
deep: bool = False,
) MutableSequence[T][source]
sorted_dict(
x: Mapping[K, V],
/,
*,
key: Callable[[K], Any] | None = None,
reverse: bool = False,
) Dict[K, V][source]
sum(
args: Iterable[T_SupportsAdd],
/,
*,
start: T_SupportsAdd = 0,
) T_SupportsAdd[source]
sum(
*args: T_SupportsAdd,
start: T_SupportsAdd = 0,
) T_SupportsAdd
sum(
arg0: T_SupportsAdd,
/,
*args: T_SupportsAdd,
start: T_SupportsAdd | None = 0,
) T_SupportsAdd
unflat_dict_of_dict(
dic: Mapping[str, Any],
*,
sep: str = '.',
) Dict[str, Any][source]

Unflat a dictionary.

Example 1

``` >>> dic = {

“a.a”: 1, “b.a”: 2, “b.b”: 3, “c”: 4,

} >>> unflat_dict_of_dict(dic) … {“a”: {“a”: 1}, “b”: {“a”: 2, “b”: 3}, “c”: 4} ```

unflat_list_of_list(
flatten_lst: Sequence[T],
sizes: Iterable[int],
) List[List[T]][source]

Unflat a list to a list of sublists of given sizes.

union(
*args,
start=None,
)[source]
union_dicts(
dicts: Iterable[Dict[K, V]],
) Dict[K, V][source]
union_lists(
lst_of_lst: Iterable[Iterable[T]],
) List[T][source]

Performs union of elements in lists (like set union), but keep their original order.

unzip(
lst: Iterable[Tuple[()]],
) Tuple[()][source]
unzip(
lst: Iterable[Tuple[T]],
) Tuple[List[T]]
unzip(
lst: Iterable[Tuple[T, U]],
) Tuple[List[T], List[U]]
unzip(
lst: Iterable[Tuple[T, U, V]],
) Tuple[List[T], List[U], List[V]]
unzip(
lst: Iterable[Tuple[T, U, V, W]],
) Tuple[List[T], List[U], List[V], List[W]]
unzip(
lst: Iterable[Tuple[T, U, V, W, X]],
) Tuple[List[T], List[U], List[V], List[W], List[X]]

Invert zip() function.

Example
>>> lst1 = [1, 2, 3, 4]
>>> lst2 = [5, 6, 7, 8]
>>> lst_zipped = list(zip(lst1, lst2))
>>> lst_zipped
... [(1, 5), (2, 6), (3, 7), (4, 8)]
>>> unzip(lst_zipped)
... [1, 2, 3, 4], [5, 6, 7, 8]