简体   繁体   中英

Assign values to numpy.array

Let say we create this np.array:

A = np.arange(12).reshape(3, 4)

So, A is:

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

I would like to assign some values to A like that:

A[[0,2]][:,[1,3]] = 9999

In order to obtain:

array([[ 0,  9999,  2,  9999],
       [ 4,  5,  6,  7],
       [ 8,  9999, 10, 9999]])

But this doesn't work. What is the proper way to do it?

Thanks

您可以使用np.ix_来获取那些开放式网格物体,当这些网格物体用于索引到数组中时将被broadcasted ,因此可以用于为其分配值,如下所示-

A[np.ix_([0,2],[1,3])] = 9999

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM