这是改编自此答案的一个选项:def count_consecutive(arr, n): # pad a with False at both sides for edge cases when array starts or ends with n d = np.diff(np.concatenate(([False], arr == n, [False])).astype(int)) # subtract indices when value changes from False to True from indices where value changes from True to False return np.flatnonzero(d == -1) - np.flatnonzero(d == 1)count_consecutive(a, 5)# array([2, 1, 3])