0

What is the simplest way to count the total number of repeating sequential letters in a string? For example, if I have a string 'TCAAAAAAAACAT', I know I can count the total number of 'A's using the count function count(A). However, since I only want the number of 'As' that are sequential in the string (the ones in the middle), what would I do to have the script ignore any letters that are not repeating and sequential?

Chris Lin
  • 9
  • 1
  • Some good information in here: https://stackoverflow.com/questions/25537028/run-length-encoding-in-python-with-list-comprehension – mypetlion Sep 12 '18 at 17:13
  • Another one: https://stackoverflow.com/q/885546/674039 – wim Sep 12 '18 at 17:18

2 Answers2

0

Code prints position and length of series:

s = 'TCAAAAACCABBBT'
start = 0
for i in range(1, len(s) + 1):
    if (i==len(s) or s[i] != s[i-1]):
        if (i - start > 1):
            print(start, i - start)
        start = i
>>2 5
7 2
10 3
MBo
  • 77,366
  • 5
  • 53
  • 86
-4

There is a function called count() for lists and strings, use it like that:

[“a”,”a”,”b”,”c”].count(letter)

Or

“aabc”.count(letter)
Docaro
  • 60
  • 9