numpy problem

lists at onemanifest.net lists at onemanifest.net
Mon May 23 09:10:47 EDT 2016


> 
> 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 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()

That seems to do the trick!

Thx.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.python.org/pipermail/python-list/attachments/20160523/483b36d7/attachment.sig>


More information about the Python-list mailing list