I am asking a kind of generalisation of this question:
Best way to extract elements from nested lists.
It is also somehow related to these questions:
recursive function for extract elements from deep nested lists/tuples
Scheme - find most deeply values nested lists
Scheme - find most deeply nested lists
Essentially, I have some arbitrary nested list structure, where at the bottom there are various numpy arrays that are all the same shape. I want to iterate or slice all these bottom-level arrays whilst preserving the nested list structure in which they live. It is this part about preserving the nested structure in the output which doesn't seem to be answered in these other questions.
So, for example:
A = np.ones((3,3,3))
nest = [[A,A,A],[[A,A,[A,A]],[A,A]],A]
and we want, schematically,
nest[0,...] == [[A[0,...],A[0,...],A[0,...]],[[A[0,...],A[0,...],[A[0,...],A[0,...]]],[A[0,...],A[0,...]]],A[0,...]]
or
nest[1:3,5,:] == [[A[1:3,5,:],A[1:3,5,:],A[1:3,5,:]],[[A[1:3,5,:],A[1:3,5,:],[A[1:3,5,:],A[1:3,5,:]]],[A[1:3,5,:],A[1:3,5,:]]],A[1:3,5,:]]
I'm sure some clever recursive function or something can do this, my brain is just not coming up with it right now...
I guess it would also be best if this returns views onto the bottom level arrays rather than copies parts of them.
edit: Perhaps something like this can work: https://stackoverflow.com/a/43357135/1447953. That method would require that numpy slicing operations be converted into functions somehow, which I guess can be done on a case-by-case basis, so perhaps this is the way to go.