Skip to content

ding.utils.collection_helper

ding.utils.collection_helper

iter_mapping(iter_, mapping)

Overview

Map a list of iterable elements to input iteration callable

Arguments: - iter_(:obj:_IterType list): The list for iteration - mapping (:obj:Callable [[_IterType], _IterTargetType]): A callable that maps iterable elements function. Return: - (:obj:iter_mapping object): Iteration results Example: >>> iterable_list = [1, 2, 3, 4, 5] >>> _iter = iter_mapping(iterable_list, lambda x: x ** 2) >>> print(list(_iter)) [1, 4, 9, 16, 25]

Full Source Code

../ding/utils/collection_helper.py

1from typing import Iterable, TypeVar, Callable 2 3_IterType = TypeVar('_IterType') 4_IterTargetType = TypeVar('_IterTargetType') 5 6 7def iter_mapping(iter_: Iterable[_IterType], mapping: Callable[[_IterType], _IterTargetType]): 8 """ 9 Overview: 10 Map a list of iterable elements to input iteration callable 11 Arguments: 12 - iter_(:obj:`_IterType list`): The list for iteration 13 - mapping (:obj:`Callable [[_IterType], _IterTargetType]`): A callable that maps iterable elements function. 14 Return: 15 - (:obj:`iter_mapping object`): Iteration results 16 Example: 17 >>> iterable_list = [1, 2, 3, 4, 5] 18 >>> _iter = iter_mapping(iterable_list, lambda x: x ** 2) 19 >>> print(list(_iter)) 20 [1, 4, 9, 16, 25] 21 """ 22 for item in iter_: 23 yield mapping(item)