How to fix 'String index out of range' err

2019-06-06 12:48发布

I am trying to write a code which replaces repeating symbols in a string with a symbol and number of its repeats (like that: "aaaaggggtt" --> "a4g4t2"). But I'm getting string index out of range error((

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)

line 6, in if seq[i] == seq[j]: IndexError: string index out of range

UPD: After changing len(seq) to len(seq)-1 there is no more string index error, but the code still doesn't work. Input: aaaaggggtt
Output:aaaaggggtt (same)

3条回答
We Are One
2楼-- · 2019-06-06 13:00

Using Counter, you get a dictionary with the caracters as key and the number of repetition of that caracter as value. Then I iterated the dictionary, creating a list in witch each element was made by key + number of that character. I joined the list obtaining the string you were looking for.

from collections import Counter

a = "aaaaggggtt"
x = Counter(a)

w = ""
for k in x:
    w = [k+str(x[k]) for k in x]
w = "".join(w)
print(w)

OUTPUT:

a4g4t2

查看更多
Deceive 欺骗
3楼-- · 2019-06-06 13:10

You can iterate over the string, keeping a running counter and create your string as you go

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)

Or you can approach this using 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)

The output will be

a4g4t2
查看更多
Viruses.
4楼-- · 2019-06-06 13:17

simple way:

str1 = 'aaaaggggtt'

set1 = set(str1)

res = ''

for i in set1:

    res+=i+str(str1.count(i))

print(res)
查看更多
登录 后发表回答