Suppose we have strings of the type:
test= '--a-kbb-:xx---xtx:-----x--:---g-x--:-----x--:------X-:XXn-tt-X:l--f--O-'
that is, they are always composed of 8 sections separated by :, so one could split the string into a list with each element corresponding to a section:
testsep = test.split(':')
giving
['--a-kbb-', 'xx---xtx', '-----x--', '---g-x--', '-----x--', '------X-', 'XXn-tt-X', 'l--f--O-']
Now I want to check if the string test is such that there are in 3 consecutive sections an x occurring at the same position of the section. For example, with the test given above, we find at least one such case: counting from 1, sections 2,3 and 4 contain an x at the same position, namely at index 6. Therefore, our test string here matches the wanted pattern.
- Is there a simple (maybe functional way) of checking for such patterns given strings always composed with the formatting above?
The naive approach would be to split, then loop through all sections and see if there are consecutive sections having x at each possible position (first index 1, 2, ...up to 8), but that wouldn't be very python-like.