numpy problem

Michael Selik michael.selik at gmail.com
Mon May 23 19:00:30 EDT 2016


On Mon, May 23, 2016 at 9:12 AM <lists at onemanifest.net> wrote:

> > On 23 mei 2016, at 14:19, Peter Otten <__peter__ at web.de> wrote:
> > lists at onemanifest.net wrote:
> >
> >> I've got a 2D array
> >> And an array of indexes that for shows which row to keep for each column
> >> of values:
> >>
> >> keep = np.array([2, 3, 1, 9, 2])
> >>
> >> So, the result should be an array like array([ values[2,0], values[3,1],
> >> values[1,2], values[9,3], values[2,4] ]) == np.array([92, 62, 38, 81,
> 44])
> >>
> > values[keep].diagonal()
>
> That seems to do the trick!
>

To clarify, the fancy index is selecting all rows you want to keep, which
gives you a 5x5 array (repeating rows if necessary). Then you are getting
the diagonal of that selection.

If that was already clear, my apologies for the noise.



More information about the Python-list mailing list