2017-07-15 41 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