I want to get the start and finish index of the first consecutive sequence of the same character string in python
'aaabca' -> (0, 2)
'helllooo' -> (2, 4)
'hellooo' -> (2,3)
'abcd' -> (-1, -1)
is there a super clean way to accomplish that?
You could use a regex
that find the repetition of a char ( (\\w)\\1+
), then get the position of the match (using m.start()
and m.end()
)
values = ['aaabca', 'helllooo', 'hellooo', 'abcd']
for value in values:
m = re.search(r'(\w)\1+', value)
if m:
print(f'{value:10s}{str((m.start(), m.end() - 1)):10s}{m.group(0)}')
else:
print(f'{value:10s}{str((-1, -1)):10s}')
Giving
aaabca (0, 2) aaa
helllooo (2, 4) lll
hellooo (2, 3) ll
abcd (-1, -1)
To change the type of char to search a repetition on, replace the \\w
(\\d)\\1+
repetition of a digit (.)\\1+
repetition of any char ([az])\\1+
repetition of a lower case Here's one way to do it
x = "helllooo"
count = 0
start = -1
end = -1
for i in range(len(x)-1):
if x[i] == x[i+1]:
if count == 0:
start = i
count += 1
end = start + count
else:
if count > 0:
break
count = 0
print(start, end)
My first answer on stackoverflow. I hope you can give me rep :) Totally works have a nice day
word_list=['aaabca','helllooo','hellooo','abcd']
def find(word):
first_char=[]
index_list=[]
for n,i in enumerate(word):
if n+1<len(word):
if i==word[n+1]:
first_char.append(i)
while first_char[0]==i:
index_list.append(n)
index_list.append(n+1)
break
try:
print(index_list[0],index_list[-1])
except:
print(-1,-1)
for word in word_list:
find(word)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.