将字符串拆分为相同字母的块 [英] Split string into chunks of same letters
问题描述
这很简单,我就是做不到!在这个例子中,我想要做的就是将下面的字符串分成彼此相邻的相同字母块,例如在下面的示例中:test = "AAATGG", 将被拆分为 "AAA","T","GG".我一直在尝试不同的方法,下面举一个例子.感谢您的帮助.
this is easy, I just can't do it! In this example, all I want to do is split the string below into chunks of same letters that are beside each other, e.g. in the below example: test = "AAATGG", would be split into "AAA","T","GG". I've been trying different ways, one example below. I'd appreciate the help.
我知道这个想法是遍历字符串,如果下一个字母与当前字母相同,则继续,否则,中断并打印并重新开始,我无法正确实现它.
I know the idea is to go through the string, if the next letter is the same as the current letter, continue on, else, break and print and start again, I just can't implement it properly.
test = "AAATGG"
TestDict = {}
for index,i in enumerate(test[:-1]):
string = ""
if test[index] == test[index+1]:
string = i + test[index]
else:
break
print string
推荐答案
一种方法是使用 itertools
中的 groupby
:
One way is to use groupby
from itertools
:
from itertools import groupby
[''.join(g) for _, g in groupby(test)]
# ['AAA', 'T', 'GG']
这篇关于将字符串拆分为相同字母的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!