用Python编码字符串
我试图解决一个编程问题,将字符串转换为以下形式: 输入:AABBBCC 输出:a4b3c2 我的代码如下:用Python编码字符串,python,algorithm,Python,Algorithm,我试图解决一个编程问题,将字符串转换为以下形式: 输入:AABBBCC 输出:a4b3c2 我的代码如下: 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
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
但我得到以下例外情况:
回溯最近一次呼叫上次:
文件encode.py,第30行,在
打印编码
文件encode.py,encode中的第13行
而s[i]==s[j]:
索引器错误:字符串索引超出范围
我无法理解这里的错误。有人能帮我吗?其他人已经指出,您没有检查内部循环中的列表边界 请注意,您可以使用正则表达式import re和list comprehension进行字符串转换,如下所示:
''.join([ch + str(len(m)) for m, ch in re.findall(r"((.)\2*)", "aaaabbbcc")])
您的代码工作正常。唯一的问题是,如果字符串有一个像aaabbd d1这样的字母,则不会返回。 你也可以试试re 您可以将字符串转换为集合。然后可以迭代集合并调用计数来查找重复字符数
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.
您可以使用以下功能:
import itertools
result = ""
for k, group in itertools.groupby('aaaabbbcc'):
result += '%s%d' % (k, len(list(group)))
print(result)
>>> a4b3c2
你可以用
你没有检查j是否在内环内越界…可能是重复的,谢谢,收到了!请注意,这对重复的组(如aaabaa)不起作用。
import itertools
result = ""
for k, group in itertools.groupby('aaaabbbcc'):
result += '%s%d' % (k, len(list(group)))
print(result)
>>> a4b3c2
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"