fork(3) download
  1. import numpy as np
  2.  
  3. A= np.array([[ 0, 1, 2],
  4. [ 1, 1, 6],
  5. [ 2, 2, 10],
  6. [ 3, 2, 14]])
  7. # Find the split indices
  8. i = np.nonzero(np.diff(A[:, 1]))[0] + 1
  9. i = np.insert(i, 0, 0)
  10. # Compute the result columns
  11. c0 = np.arange(i.size)
  12. c1 = A[i, 1]
  13. c2 = np.add.reduceat(A[:, 2], i)
  14. # Concatenate the columns
  15. result = np.c_[c0, c1, c2]
  16.  
  17. print(result)
Success #stdin #stdout 0.13s 24736KB
stdin
Standard input is empty
stdout
[[ 0  1  8]
 [ 1  2 24]]