espnet2.asr.state_spaces.utils.to_dict
Less than 1 minute
espnet2.asr.state_spaces.utils.to_dict
espnet2.asr.state_spaces.utils.to_dict(x, recursive=True)
Convert a Sequence or Mapping object to a dictionary.
This function takes an input object x, which can be a list, tuple, dictionary, or any other type. If x is a list or tuple, it is converted to a dictionary with indices as keys. If x is a dictionary, its values are recursively converted to dictionaries if recursive is set to True. If x is neither a list nor a dictionary, it is returned unchanged.
- Parameters:
- x (Sequence or Mapping) – The input object to be converted.
- recursive (bool) – If True, recursively convert nested dictionaries/lists. Default is True.
- Returns: A dictionary representation of the input object, : or the input itself if it is neither a list nor a dictionary.
- Return type: dict
Examples
>>> to_dict([1, 2, 3])
{0: 1, 1: 2, 2: 3}
>>> to_dict({'a': 1, 'b': [2, 3]})
{'a': 1, 'b': {0: 2, 1: 3}}
>>> to_dict('string')
'string'
>>> to_dict({'a': 1, 'b': {'c': 2}}, recursive=True)
{'a': 1, 'b': {'c': 2}}
NOTE
The function distinguishes between lists, dictionaries, and other types to ensure appropriate conversion.
- Raises:TypeError – If the input type is unsupported.