1

I have a binary list of 20 numbers : a=[1,1,1,1,0,1,1,0,1,1,1,0,1,1,1,1,1,0,0,0,1,1,1,0,1,1,0,1,1,0], my goal is to count the maximum number of consecutive occurrences of 1 on each 5 elements and store it on a list, in this case the output would be: out= [4,5,3], because on the first 10 elements we have 4 consecutive 1s as a maximum, and on the second 10 elements we have 5 consecutive 1s and the last we have 3.

My current function is the following:

`def count_ones(lista):
   counts=[]
   longest=0
   current=0
   for n in range(0,len(lista),10):
        for i in range(n,n+10):
           if i>= len(lista): break
           if lista[i]==1:
               current+=1
           else:
               longest= max(longest , current)
               current=0
        counts.append(longest)
return (counts)
        `

which is not working properly

  • Does this answer your question? [Max Consecutive Ones](https://stackoverflow.com/questions/55771644/max-consecutive-ones) – Reti43 May 12 '21 at 16:41
  • The cleanest way to do what you want is with itertools.groupby and then find the longest group of 1s. Example [here](https://stackoverflow.com/questions/6352425/whats-the-most-pythonic-way-to-identify-consecutive-duplicates-in-a-list). – Reti43 May 12 '21 at 16:47
  • @Reti43 my struggle is iterating from 10 to 10 elements, it helps me on the counting part, but I would like to incorporate it whole in one function as I will be using it on other functions – user14866118 May 12 '21 at 17:06
  • It would be better to have a function to find the consecutive 1s in a whole list and then pass slices to it with `[function(a[i:i+10]) for i in range(0, len(a), 10]`. – Reti43 May 12 '21 at 17:08

2 Answers2

1

Easy code here. Credits https://stackoverflow.com/a/55772095/2695448

 def findMaxConsecutiveOnes(nums):
        slow, fast, glo_max, loc_max = 0, 0, 0, 0
        while fast < len(nums):
            if nums[fast] == 0:
                loc_max = fast - slow  
                glo_max = max(glo_max, loc_max)
                slow = fast + 1      # need to add one more because we haven't incremented fast yet

            fast += 1
        loc_max = fast - slow        # end check for cases that end with 1
        return max(loc_max, glo_max)
a = [1,1,1,1,0,1,1,0,1,1,1,0,1,1,1,1,1,0,0,0,1,1,1,0,1,1,0,1,1,0]

[findMaxConsecutiveOnes(a[i:i+10]) for i in range(0,len(a),10)]
mad_
  • 8,121
  • 2
  • 25
  • 40
  • 1
    Why didn't you flag this as a duplicate if all you were going to do was reference another question? I could at least see the merit if you explained where the OP went wrong. – Reti43 May 12 '21 at 16:41
  • could you incorporate it in only one function? i will be passing it on another function and it would help – user14866118 May 12 '21 at 17:08
1

you can use itertools groupby:

If you want to count only 1's:

from itertools import groupby

n = 10 # specify the sublist size.
result = [max(len(list(k)) for g, k in groupby(item) if g == 1)
          for item in (a[i:i + n] for i in range(0, len(a), n))]

The below code will look for the max size of consecutive occurrence of any element within a sublist.

from itertools import groupby

n = 10 # specify the sublist size.
result = [max(len(list(k)) for _, k in groupby(item))
          for item in (a[i:i + n] for i in range(0, len(a), n))]
print(result) # [4, 5, 3]
Nk03
  • 14,699
  • 2
  • 8
  • 22