s = pd.Series(arr)
out = (s.groupby(s.ne(s.shift()).cumsum(), sort=False) # group consecutive values
.agg({'first', 'size'}) # get value and count
.groupby('first', sort=False)['size'].max() # max count per value
.reset_index().to_numpy() # back to numpy
)
from collections import defaultdict
arr=np.array([1,1,1,2,2,3,4,4,4,4,4,2,4,4])
prev = None
max_consecutive_count = defaultdict(int)
running_count = 0
for val in arr:
if val == prev or not prev:
running_count += 1
else:
if running_count > max_consecutive_count[prev]:
max_consecutive_count[prev] = running_count
running_count = 1
prev = val
else:
if running_count > max_consecutive_count[val]:
max_consecutive_count[val] = running_count
max_consecutive_count_arr = [[k,v] for k,v in max_consecutive_count.items()]
2条答案
按热度按时间pw9qyyiw1#
这对于pandas来说很容易做到:
对于纯numpy,它稍微复杂一些:
输出量:
使用纯python和
itertools.groupby
:输出:
[(1, 3), (2, 2), (3, 1), (4, 5)]
定时比较
使用随机数组作为输入(
np.random.randint(1, 5, size=N)
)。nafvub8i2#
迭代每个值,并记录连续值的数量。如果该值发生变化,则更新最大计数(如果先前的运行计数更高),然后将运行计数重置为1。最大计数使用defaultdict存储,然后转换为数组。