[Numpy-discussion] Useless but tricky exercise

Nicolas P. Rougier Nicolas.Rougier at inria.fr
Thu Nov 3 14:29:10 EDT 2016


Hi all,

Given an array V that is a view of a base B, I was wondering if it is possible
to find a (string) index such that `V = B[index]`

For example:

```
B = np.arange(8*8).reshape(8,8)
V = B[::2,::2]

index = find_view(B,V)
print(index)
"::2,::2"

print(np.allclose(V, eval("B[%s]" % index)))
True
```


I wrote a solution at https://gist.github.com/rougier/b8c2256434b3a4a4271260cd4cc6cbc7 
(not very thoroughly tested) but maybe there are better ways to do that (like a magic numpy call ?)


(no use-case at all, only for teaching)


Nicolas




More information about the NumPy-Discussion mailing list