简体   繁体   English

在Python中查找列表中任何项的最连续序列

[英]Finding the most consecutive sequence of any item of a list in Python

How can I find the most consecutive sequence of any item(integers) of a list in Python? 如何在Python中找到列表中任何项(整数)的最连续序列?

my_list = [1, 3, 2, 2, 4, 5, 5, 5, 5, 6, 1, 5, 5]

I want to extract [5, 5, 5, 5] from the list. 我想从列表中提取[5, 5, 5, 5]

NB Please, correct me if I've written anything wrong. 注意:如果我写错了什么,请纠正我。 :-) :-)

Use itertools.groupby() and max() : 使用itertools.groupby()max()

In [1]: my_list = [1, 3, 2, 2, 4, 5, 5, 5, 5, 6, 1, 5, 5]

In [2]: from itertools import groupby

In [4]: max([list(g) for _, g in groupby(my_list)], key=len)
Out[4]: [5, 5, 5, 5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM