nested_argwhere#
- ivy.nested_argwhere(nest, fn, check_nests=False, to_ignore=None, _index=None, _base=True, stop_after_n_found=None)[source]#
Check the leaf nodes of nested x via function fn, and returns all nest indices where the method evaluates as True.
- Parameters:
nest (
Iterable
) – The nest to check the leaves of.fn (
Callable
) – The condition function, returning True or False.check_nests (
bool
, default:False
) – Whether to also check the nests for the condition, not only nest leaves. Default isFalse
.to_ignore (
Optional
[Union
[type
,Tuple
[type
]]], default:None
) – Types to ignore when deciding whether to go deeper into the nest or not_index (
Optional
[List
], default:None
) – The indices detected so far. None at the beginning. Used internally, do not set manually._base (
bool
, default:True
) – Whether the current function call is the first function call in the recursive stack. Used internally, do not set manually.stop_after_n_found (
Optional
[int
], default:None
) – to stop after some needed indices are found.
- Return type:
Union
[Iterable
,bool
]- Returns:
ret – A set of indices for the nest where the function evaluated as True.
Examples
With
List
input:>>> nest = [[[1, -2, 3], 19], [[9, -36, 80], -10.19]] >>> fun = ivy.abs >>> nested_indices = ivy.nested_argwhere(nest, fn=fun) >>> print(nested_indices) [ [0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1], [1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1] ]
With
Tuple
input:>>> nest = ([-5, 9, 2], [0.3, 4.]) >>> fun = ivy.abs >>> nested_indices = ivy.nested_argwhere(nest, fn=fun, stop_after_n_found=4) >>> print(nested_indices) [[0, 0], [0, 1], [0, 2], [1, 0]]
With
Dict
input:>>> nest={'a': [2., 0.6, -2.], 'b': [1., 4., 1.9], 'c': [9.4]} >>> fun = ivy.abs >>> nested_indices = ivy.nested_argwhere(nest, fn=fun) >>> print(nested_indices) [ ['a', 0], ['a', 1], ['a', 2], ['b', 0], ['b', 1], ['b', 2], ['c', 0] ]