sidx = L.argsort()
col_idx, grp_start_idx = np.unique(L[sidx], return_index=True)
B_out = np.zeros((len(A), n_cols))
B_out[:, (col_idx)] = np.add.reduceat(A[:, (sidx)], grp_start_idx, axis=1)
Numpy collapse columns according to list