Home

Array Operations

Array Construction

Arrays should be constructed using array, zeros or empty (refer to the See Also section below). The parameters given here refer to a low-level method (ndarray(…)) for instantiating an array.

Iteration

a = np.arange(0,60,5)
a = a.reshape(3,4)

for x in np.nditer(a):
   print x,

Iteration order

Iteration order is selected from memory layout so Transpose of a matrix would result in same order

To change the iteration order nditer has optional parameters.

np.nditer(a, order = 'F'): 

Read-Write

with np.nditer(a, op_flags=['readwrite']) as it:
  for x in it:
    x[...] = 2 * x

Accessing Index

it = np.nditer(a, flags=['f_index'])
while not it.finished:
  print("%d <%d>" % (it[0], it.index), end=' ')
  it.iternext()

Multi Index

it = np.nditer(a, flags=['multi_index'], op_flags=['writeonly'])
with it:
   while not it.finished:
       it[0] = it.multi_index[1] - it.multi_index[0]
       it.iternext()

Appending

np.append(array, values, axis)

Link To Doc