如果我有aaabbbccc
,我想将其更改为a3b3c3
。
我正在使用if语句..但它看起来效率太低。
也许正则表达式会有所帮助,但只能连续搜索连续统一的正则表达式?
如果我有aaabbbcccaaa
,那么我想更改它们a3b3c3a3
列出这个..这意味着算法只搜索“连续并计算它们”变为整数。
任何提示都将受到赞赏。
def comp(string):
index = []
for i in range(len(string)):
try:
if string[i] is not string[i+1]:
index.append(i)
except:
pass
first = string[index[0]] + str(index[0]+1)
print(first)
message_comp = [first]
for i in range(1, len(message_comp)):
message_comp.append(message[index[i]]*(index[i-1]+1))
final = ''.join(message_comp)
return final
答案 0 :(得分:2)
<强> itertools.groupby
强>:
创建一个从迭代
返回连续键和组的迭代器
import itertools
x = 'aaabbbcccaaa'
groups = [i + str(len([*j])) for i, j in itertools.groupby(x)]
# ['a3', 'b3', 'c3', 'a3']
join
完成:
''.join(groups)
# a3b3c3a3
如果需要, replace
删除1
:
''.join(groups).replace('1', '')
代替''.join(groups)
答案 1 :(得分:0)
也许是itertools groupby?
from itertools import groupby
s = "aaabbbcccaaa"
groups = groupby(s)
a = [(label, sum(1 for _ in group)) for label, group in groups]
b = [i for sub in a for i in sub]
print("".join(map(str,b)))
output: a3b3c3a3