我正在尝试编写代码,用符号和重复次数替换字符串中的重复符号(例如:“ aaaaggggtt”->“ a4g4t2”)。但我的字符串索引超出范围错误((
seq = input()
i = 0
j = 1
v = 1
while j<=len(seq)-1:
if seq[i] == seq[j]:
v += 1
i += 1
j += 1
elif seq[i] != seq[j]:
seq.replace(seq[i-v:j], seq[i] + str(v))
v = 1
i += 1
j += 1
print(seq)
第6行,在 如果seq [i] == seq [j]: IndexError:字符串索引超出范围
UPD:将len(seq)更改为len(seq)-1后,不再有字符串索引错误,但是代码仍然无法正常工作。
输入:aaaaggggtt
输出:aaaaggggtt(相同)
答案 0 :(得分:0)
您可以遍历字符串,保持计数器运行并随时创建字符串
s = 'aaaaggggtt'
res = ''
counter = 1
#Iterate over the string
for idx in range(len(s)-1):
#If the character changes
if s[idx] != s[idx+1]:
#Append last character and counter, and reset it
res += s[idx]+str(counter)
counter = 1
else:
#Else increment the counter
counter+=1
#Append the last character and it's counter
res += s[-1]+str(counter)
print(res)
或者您可以使用itertools.groupby
from itertools import groupby
s = 'aaaaggggtt'
#Count numbers and associated length in a list
res = ['{}{}'.format(model, len(list(group))) for model, group in groupby(s)]
#Convert list to string
res = ''.join(res)
print(res)
输出将为
a4g4t2
答案 1 :(得分:0)
简单方法:
str1 = 'aaaaggggtt'
set1 = set(str1)
res = ''
for i in set1:
res+=i+str(str1.count(i))
print(res)