count consecutive elements
Bischoop
Bischoop at vimart.net
Wed Jan 13 16:20:32 EST 2021
I want to to display a number or an alphabet which appears mostly
consecutive in a given string or numbers or both
Examples
s= ' aabskaaabadcccc'
output: c
# c appears 4 consecutive times
8bbakebaoa
output: b
#b appears 2 consecutive times
I thought about set the string then and for each element loop the string
to check if equal with element set element if so count =+1 and check if
next to it is not equal add counter value to list with same index as in
set.
However I'm fighting to code it and the counting numbers are not right:
s = 'aabskaaabadcccc'
c = 0
t = set(s) # set of characters in s
li=[0,0,0,0,0,0] # list for counted repeats
for x in t:
h = t.index(x)
for i in s:
if i == x:
m = s.index(i)
c +=1
if s[m+1] != x: # if next element is not x
if c > li[h]:
li[h]= c
c = 0
for i in t:
n = t.index(i)
print(i,li[n])
--
Thanks
More information about the Python-list
mailing list