count consecutive elements

Bischoop Bischoop at vimart.net
Wed Jan 13 21:06:33 EST 2021


On 2021-01-13, Bischoop <Bischoop at vimart.net> wrote:

I know what was wrong:

>             m = s.index(i)

I forgot that m will return first index of i.

So tried that way but still getting out of index although I that that
I'm making sure not to get out of index.

s = 'aabskaaabadcccch'

c = 0
t = list(set(s)) # list of characters in s
li=[0,0,0,0,0,0]  # list for counted repeats
print(t)
for x in t:
    h = t.index(x)
    for index, i in enumerate(s):
        maximus = len(s)
        if i == x:
            c += 1
            if index < maximus:
                if s[index +1] != x:  # if next element is not x
                    if c > li[h]:   #update c if bigger than existing
                        li[h] = c
                    c = 0
            else:
                if c > li[h]:
                    li[h] = c


for i in t:
    n = t.index(i)
    print(i,li[n])

print(f'{s[li.index(max(li))]} appears {max(li)} consecutive times')




More information about the Python-list mailing list