找出最长的连续数字序列的长度 [英] find the length of the longest consecutive series of numbers
本文介绍了找出最长的连续数字序列的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的numpy数组[1,1,1,-1,-1,1,-1,1,1,-1,-1,-1,1,-1]
我想找到最长连续序列1s或-1s的长度.在示例中,应该为3
I have a numpy array like this [1,1,1,-1,-1,1,-1,1,1,-1,-1,-1,1,-1]
I'd like to find the length of the longest consecutive series of either 1s or -1s. In the example, it should be 3
推荐答案
在纯Python中
>>> from itertools import groupby
>>> L = [1,1,1,-1,-1,1,-1,1,1,-1,-1,-1,1,-1]
>>> max(sum(1 for i in g) for k,g in groupby(L))
3
这篇关于找出最长的连续数字序列的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文