1

I'm sorry the title is confusing, but I couldn't word it any better.

So, let's say I have this list:

My_list = [1,1,1,0,0,0,1,1,1,1,0,0]

How do display the highest number of consecutively repeating 1s and 0s?

I want to display 4 as for the longest chain of consecutively repeating 1s is 4.

And display 3 as for the longest chain of consecutively repeating 0s is 3.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 1
    Word suggestion- consecutively. "conscivately" isn't even a word. – Sid Oct 27 '19 at 09:58
  • 4
    What have you tried thus far? There are many possible solutions to this problem. – Cheri Oct 27 '19 at 10:02
  • 1
    Honestly I have no idea what to try, I'm still a beginner. I've did the research and came up with no results. One of my friends suggested I ask here so I did – user12280978 Oct 27 '19 at 10:04
  • 1
    I did that yesterday, duplicate question of [link](https://stackoverflow.com/questions/58575022/highest-number-of-consecutively-repeating-values-in-a-list/58575095#58575095). Just delete max in print... – Mouse on the Keys Oct 27 '19 at 10:04
  • 1
    Possible duplicate of [Highest number of consecutively repeating values in a list](https://stackoverflow.com/questions/58575022/highest-number-of-consecutively-repeating-values-in-a-list) – Mike Scotty Oct 27 '19 at 10:07
  • This should work: https://stackoverflow.com/questions/38485735/python-find-sequence-of-same-characters – PySeeker Oct 27 '19 at 10:08

4 Answers4

2

Similar previous problem searching would find

from itertools import groupby

mylist = [1,1,1,0,0,0,1,1,1,1,0,0]
#count_ones = (max(list(group),  for _, group in groupby(mylist)), len)

runs = [list(group) for _, group in groupby(mylist)] #create sublists of same values (ones of zeros)
ones = [g for g in runs if g[0]  == 1]  # ones only sublists
zeros = [g for g in runs if g[0]  == 0] # zeros only sublists

print(len(max(ones,key=len)))           # max sublists of ones -> 4
print(len(max(zeros,key=len)))          # max sublists of zeros -> 3
DarrylG
  • 16,732
  • 2
  • 17
  • 23
2

A simple solution, probably something more elegant in itertools somewhere.

x = [1,1,1,0,0,0,1,1,1,1,0,0,0,0,0]

d = {}
c = x[0]
c_cnt = 1

for i in x[1:]:
    if i == c:
        c_cnt +=1
    else:
        d[c] = max(d.get(c,0),c_cnt)
        c=i
        c_cnt=1
d[c] = max(d.get(c,0),c_cnt)

print(d)
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
1

Just to be in general case not just 1 or 0:

from itertools import groupby

My_list = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0]
result = {}
for element, groups in groupby(My_list):
    count = sum(1 for _ in groups)
    if element in result:
        if count > result[element]:
            result[element] = count
    else:
        result[element] = count

for e, count in result.items():
    print('longest chains for {} is {}'.format(e, count))

OUTPUT:

longest chains for 1 is 4
longest chains for 0 is 3

I tried to keep the code simple to understand the logic.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • Instead of `len(list(groups))` you could write `sum(1 for _ in groups)` which should be more space efficient. – Alex R Oct 27 '19 at 10:14
  • @AlexR thanks for the suggest of curse it's a lot more better. I was just searching for this https://stackoverflow.com/questions/390852/is-there-any-built-in-way-to-get-the-length-of-an-iterable-in-python/28385557 – Charif DZ Oct 27 '19 at 10:17
0

I have some code that does it. I'm not sure if this is the best approach for very long lists, but it should work for the list you posted.

ps. No numpy :)

def unique(list1): 

    # intilize a null list 
    unique_list = [] 

    # traverse for all elements 
    for x in list1: 
        # check if exists in unique_list or not 
        if x not in unique_list: 
            unique_list.append(x) 
    # print list 
    return unique_list


My_list = [1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1]

#Create an array with the unique values from your list
unique_list = unique(My_list)
#Create a list with size = amount of unique values
zeros = [0 for _ in range(len(unique_list))]


count = 1
for i in range(0,len(My_list)-1):
    #Is the following consecutive value equal to the current value? 
    if (My_list[i] == My_list[i+1]) and (i != len(My_list)-2): 
        #count how many consecutive repetitions you have
        count = count + 1
    else:
        idx = unique_list.index(My_list[i])
        #If it's the highest amount of repetitions, save it
        if zeros[idx] < count:
            zeros[idx] = count    
        count = 1

for i in range(0, len(unique_list)):
    print("Highest consecutive "+ str(unique_list[i]) + "'s are: " + str(zeros[i]))
Ivan
  • 1,352
  • 2
  • 13
  • 31