PyREx

pyrex.internal_functions.flatten

pyrex.internal_functions.flatten(iterator, dont_flatten=())

Flattens an iterator to iterate over all elements individually.

Flattens all iterable elements in the given iterator recursively and yields the resulting flat iterator. Can optionally not flatten certain classes. Will not flatten strings or bytes to avoid recursion errors.

Parameters
iteratoriterable object

Iterable object to flatten.

dont_flattentuple_like, optional

Tuple (or similar) of classes which should not be flattened.

Yields
elementany

Each element of iterator with sub-iterators expanded out.

Notes

Since str and bytes objects are always considered iterable despite their length, these objects will not be flattened and will remain intact.

If a class is asked not to be flattened, any sub-iterators contained in an iterator of that class will not be flattened either (see examples).

Examples

>>> list(flatten([1, 2, (3, 'four', [5, 6], 7), [8, 9]]))
[1, 2, 3, 'four', 5, 6, 7, 8, 9]
>>> list(flatten([1, 2, (3, 'four', [5, 6], 7), [8, 9]], dont_flatten=(tuple,)))
[1, 2, (3, 'four', [5, 6], 7), 8, 9]
>>> list(flatten([1, 2, [3, 'four', (5, 6), 7], [8, 9]], dont_flatten=(tuple,)))
[1, 2, 3, 'four', (5, 6), 7, 8, 9]

PyREx

A Python package for simulation of neutrinos and radio antennas in ice. Version 1.10.0

Navigation