简体   繁体   中英

Python NumPy: referencing columns

Why is array1[:][1] != array1[:,1] ?

For example:

array1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
array1[1] ## Output: array([4,5,6]) as expected
array1[:,1] ## Output: array([2, 5, 8]) as expected
array1[:][1] ## Output: array([4,5,6]) which isn't what I expected!

When using double bracket referencing, is the array1[:] component executed first returning the full 2D array? Therefore array1[:][1] == array1[1] ?

NumPy will interpret a[:] as a copy of the array instead of the set of 'rows'. Basic slicing is only analogous to successive slicing until : entries appear. From the docs (section 1.4 - Indexes):

Basic slicing with more than one non-: entry in the slicing tuple, acts like repeated application of slicing using a single non-: entry, where the non-: entries are successively taken (with all other non-: entries replaced by :). Thus, x[ind1,...,ind2,:] acts like x[ind1][...,ind2,:] under basic slicing.

There is an implied complication when : entries get handled.

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