Create mask by first positions only
问题 I have array: a = np.array([[ 0, 1, 2, 0, 0, 0], [ 0, 4, 1, 35, 0, 10], [ 0, 0, 5, 4, 0, 4], [ 1, 2, 5, 4, 0, 4]]) I need select only from first consecutive 0 in each row: [[ True False False False False False] [ True False False False False False] [ True True False False False False] [ False False False False False False]] I try: a[np.arange(len(a)), a.argmax(1): np.arange(len(a)), [0,0,0]] = True But this is wrong. 回答1: You can use np.cumsum . Assumption: you are looking for zeros only at