numpy problem

Peter Otten __peter__ at web.de
Mon May 23 08:19:28 EDT 2016


lists at onemanifest.net wrote:

> I've got a 2D array with values:
> 
> values = np.array(
> [[ 20, 38,  4,  45, 65],
>  [ 81, 44, 38,  57, 92],
>  [ 92, 41, 16,  77, 44],
>  [ 53, 62,  9,  75, 12],
>  [ 58,  2, 60, 100, 29],
>  [ 63, 15, 48,  43, 71],
>  [ 80, 97, 87,  64, 60],
>  [ 16, 16, 70,  88, 80],
>  [ 19,  1, 73,  39, 97],
>  [ 48,  3, 27,  81, 14]])
> 
> 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])
> 
> Can this be accomplished in a vectorized manner?

How about

values[keep].diagonal()




More information about the Python-list mailing list