Search code examples
pythonstringindexingsplice

Consecutive values in strings, getting indices


The following is a python string of length of approximately +1000.

string1 = "XXXXXXXXXXXXXXXXXXXXXAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBB........AAAAXXXXX"
len(string1)  ## 1311

I would like to know the index of where the consecutive X's end and the non-X characters begin. Reading this string from left to right, the first non-X character is at index location 22, and the first non-X character from the right is at index location 1306.

How does one find these indices?

My guess would be:

for x in string1:
    if x != "X":
        print(string.index(x))

The problem with this is it outputs all indices that are not X. It does not give me the index where the consecutive X's end.

Even more confusing for me is how to "check" for consecutive X's. Let's say I have this string:

string2 = "XXXXAAXAAAAAAAAAAAAAAABBBBBBBBBBBBBB........AAAAXXXXX"

Here, the consecutive X's end at index 4, not index 7. How could I check several characters ahead whether this is really no longer consecutive?


Solution

  • If I understood well your question, you just do:

    def getIndexs(string):
      lst =[]
      flag = False
      for i, char in enumerate(string):
    
        if char == "x":
          flag = True
    
        if ((char != "x") and flag):
          lst.append(i-1)
          flag = False
    
    
      return lst
    
    
    print(getIndexs("xxxxbbbxxxxaaaxxxbb"))
    

    [3, 10, 16]