Highest number of consecutively repeating values in a list

2024/7/7 6:09:43

Let's say I have this list:

List= [1,1,1,0,0,1,1,1,1,1]

How do I display the highest number of repeating 1s in a row? I want to return 5.

Answer

I don`t really understand the question, but if you want the highest number of consecutive elements in list, maybe something like this

from itertools import groupbylist = [1,1,1,0,0,1,1,1,1,1]
count_cons_val = [sum(1 for _ in group) for _, group in groupby(list)]
print(max(count_cons_val))

Output:

5
https://en.xdnf.cn/q/120446.html

Related Q&A

Python Maze Generation

I am trying to make a python maze generator but I keep getting an IndexError: list index out of range. Any ideas? Im kinda new to this stuff so I was using the code from rosetta code on maze generatio…

Why does Pythons `any` function not return True or False? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Write a list of lists into csv in Python [duplicate]

This question already has answers here:Writing a Python list of lists to a csv file(12 answers)Closed 5 years ago.I have a list of lists of pure data like thisa=[[1,2,3],[4,5,6],[7,8,9]]How can I write…

Upper limit of points pyplot

Just being a curious George here. But Im handling 279 million data points (x,y) and am wondering if pyplot can scatter such a number?Thanks.

how to delete the u and before the of database table display by python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

python list permutations [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:How to generate all permutations of a list in Python I am given a list [1,2,3] and the task is to create all the possible …

Repeating Characters in the Middle of a String

Here is the problem I am trying to solve but having trouble solving:Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a n…

Remove a big list of of special characters [duplicate]

This question already has answers here:Remove specific characters from a string in Python(27 answers)Closed 5 years ago.I want to remove each of the following special characters from my documents: symb…

How to change a html page from flask to Django [duplicate]

This question already has an answer here:How to specify URLs in Django templates?(1 answer)Closed 7 years ago.I am working on an app that requires changing a flask template to that of Django.How to ch…

How to get the text of Checkbuttons?

Checkbuttons gets generated dynamically and they are getting text from a python list. I need a logic for capturing selected checkbuttons text . As per my research everywhere they are returning the stat…