Finding ranges of specific character in a string
Question: I am working with python-3.x on windows 7. i have a string consists of
millions of characters. consider for example

    
    
    ATCGNNNATCGATNNNNNATCGANTCG
    

I want to get the ranges which are N. in here [[4,7],[13,18],[23,24]]. I can
not just take positions of Ns and then convert them to ranges because its a
huge data and this method will be too slow. It seems such an easy problem but
actually no good way comes to my mind. Is there a fast way to do this?


Answer: Not sure how this scales to a string of millions of characters, but you could
try [regular expressions](https://docs.python.org/3/howto/regex.html):

    
    
    >>> import re
    >>> data = "ATCGNNNATCGATNNNNNATCGANTCG"
    >>> spans = (g.span() for g in re.finditer('N+', data))
    >>> list(spans)
    [(4, 7), (13, 18), (23, 24)]
    

_Update:_ Tried this with randomly generated strings of A, C, G, T, and N. For
1,000,000 characters, `list(spans)` takes less than one second, and for
10,000,000 it takes about 10 seconds on my not-at-all-new computer, finding
about 1,600,000 groups of Ns.

