I have this following string:
array = "TQTDTQD"
For each D in array, is there anyway I could know how many consecutive T's or D's are before and after each D that is the same as the letter right before each D. So for the 1st D, the letter right before it is T, then I want to find how many consecutive T's are before and after 1st D. For the 2nd D, the letter right before it is Q, so i want to find # of consecutive Q's before and after 2nd D.
For instance, for the 1st D, there is 1 T before D and 1 T after showing up until there is a Q. (e.g: for the 1st D, its like "TDT")
for the 2nd D, there is one consecutive Q before or no Q after
I did start with the following Python codes to find the positions of each T,Q,D:
get_indexes = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]
T_position = get_indexes("T",array)
Q_position = get_indexes("Q",array)
D_position = get_indexes("D",array)
and then I tried this to find out T's before each D.
for each in range(0,len(D_position)):
RightBeforeD = array[D_position[each]-1]
numberofT = []
for eachofallbefore in reversed(range(0,D_position[each])):
if array[eachofallbefore] ==RightBeforeD:
numberofT.append(array[eachofallbefore])
print (numberofT)
But the results for these two D's are like 1st D:["T","T"], 2nd D: ["Q","Q"], seems like it prints out all T or Q's before each D,
- I am just wondering if I could add something like a check point to stop my for loop once the consecutive T/Q's end.
- Also I just wonder if I could use the same method for finding consecutive T/Q's AFTER each D.
many thx!