sthoooon
2/12/2017 - 8:10 AM

Array Slicing: Accessing Subarrays x[start:stop:step]

Array Slicing: Accessing Subarrays x[start:stop:step]

# One-dimensional subarrays
x[:5]  # first five elements
x[5:]  # elements after index 5
x[4:7]  # middle sub-array
x[::2]  # every other element
x[1::2]  # every other element, starting at index 1
x[::-1]  # all elements, reversed
x[5::-2]  # reversed every other from index 5

# Multi-dimensional subarrays
x2[:2, :3]  # two rows, three columns
x2[:3, ::2]  # all rows, every other column
x2[::-1, ::-1] # reversed rows and columns

b = arange(24).reshape(2,3,4) #2floors-3rows-4columns
print b.shape
print b
print b[1,1,2] #order starts from zero
print b[:,0,2]
print b[0] #select entire floor
print b[0,:,:] #samething as b[0]
print b[0, ...] #ellipsis replaces multiple colons
print b[0,1] #second row on first floor
print b[0,1,::2] #selecting every second element
print b[...,1] #select second column of all floors
print b[:,1] #select second row of all floors
print b[0,:,1] #select ground floor second column
print b[0,:,-1] #select first floor last column
print b[0,::-1,-1] #select ground floor last column reversed
print b[::-1] #reversed order by floor