简体   繁体   中英

NumPy: efficiently assign a row/column to an array

Let's say I want to assign two values, x and y to an existing matrix of shape (100,100,2) . The normal way of assigning them to a row/column would be:

my_array[row, column] = [x, y]

But I have found that the following is much more efficient:

my_array[row, column, 0] = x
my_array[row, column, 1] = y

Which totally makes sense due to the fact that the former must create a list/tuple and then assign it, while the later just assigns to a particular index without intermediate data structures being created.

I was wondering if there is a more compact way of expressing the former while preserving the efficiency of the later, something like:

np.assign(my_array[row,column], x, y)

Does it make sense?

作为确切答案,唯一想到的是:

my_array[row, column, 0], my_array[row, column, 1] = x, y

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