s = 'abbcccd'
new = ''
temp = ''
for i, letter in enumerate(s):
if i == 0:
temp += letter
continue
if letter == temp[-1]:
temp += letter
elif letter != temp[-1]:
new += temp[-1]
if len(temp) > 1:
new += str(len(temp))
temp = letter
new += temp[-1]
if len(temp) > 1:
new += str(len(temp))
我的努力。在循环期间使用临时字符串作为引用非常简单。你知道吗
因此,您应该得到:
相关问题 更多 >
编程相关推荐