I have a little question about how to check and compare two or more characters in the list in Python.
For example, I have a string "cdcdccddd". I made a list from this string to easier comparing the characters. And the needed output is: c: 1 d: 1 c: 1 d: 1 c: 2 d: 3 So it is counting the characters, if first is not the same as the second, the counter = 1, if the second is the same as third, then counter is +1 and need check the third with fourth and so on.
I got so far this algorithm:
text = "cdcdccddd"
l = []
l = list(text)
print list(text)
for n in range(0,len(l)):
le = len(l[n])
if l[n] == l[n+1]:
le += 1
if l[n+1] == l[n+2]:
le += 1
print l[n], ':' , le
else:
print l[n], ':', le
but its not working good, because its counts the first and second element, but not the second and third. For this output will be:
c : 1
d : 1
c : 1
d : 1
c : 2
c : 1
d : 3
How to make this algorithm better?
Thank you!