2017-07-15 60 views
-1

我试图解决一个编程问题的字符串转换为以下形式的字符串:
输入:aaaabbbcc
输出:a4b3c2编码与Python

我的代码如下所示:

def encode(s): 
    output = [] 
    i = 0 
    j = 1 
    while i < len(s) and j < len(s)-1 : 
     count = 1 
     output.append(s[j]) 


    while s[i] == s[j] : 
     count += 1 
     j+=1 
     i+=1 

    output.append(count) 
    i += 1 
    j += 1 


new_s = "".join(str(x) for x in output) 
return new_s 

但我得到以下例外:
回溯(最近呼叫最后):

File "encode.py", line 30, in
print encode(s)
File "encode.py", line 13, in encode
while s[i] == s[j] :
IndexError: string index out of range

我无法理解这里的错误。有人可以帮帮我吗?

+2

你不检查对于j要出界的内环内... –

+2

可能[字符串中出现字符的计数]的副本(https://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-string) – ratskin

+0

是的,谢谢了! –

回答

4

您可以使用groupby功能:

import itertools 
result = "" 
for k, group in itertools.groupby('aaaabbbcc'): 
    result += '%s%d' % (k, len(list(group))) 
print(result) 
>>> a4b3c2 
2

正如其他人表示你没有检查内部循环中的列表边界。

请注意,你可以做字符串转换使用正则表达式(import re)和列表理解,这样的:

''.join([ch + str(len(m)) for m, ch in re.findall(r"((.)\2*)", "aaaabbbcc")]) 
1

你的代码工作罚款。唯一的问题是,如果字符串具有像一个字母aaabbdd1不会回来。 您也可以尝试re

x="aaaabbbccd" 
print "".join([j+str(len(i)) for i, j in re.findall(r"((.)\2*)", x)]) 
0

您可以将您的string转换为set。您可以迭代set并致电count()查找重复字符的数量。

input_str = 'aaaabbbcc' 
# converting into set 
input_set=set(list(input_str)) 
for i in input_set: 
    print(i+str(input_str.count(i)),end='') 
# as set is unordered so output will come unordered. 
1

你可以使用collections Counter

from collections import Counter 

in_str = "aaaabbbccd" 
out_str = "" 
letters = Counter(in_str) 

for l in letters: 
    out_str += l + str(letters[l]) 

print(out_str) # a4b3c2d1 
# Note: in_str of "aabaa" will produce out_str of "a4b1" 
+0

请注意,这不适用于重复组,如'aaabaa'。 – randomir