一种“那种方式”的解决方案,仅包含基本声明:
word="100011010" #word = "1"count=1length=""if len(word)>1: for i in range(1,len(word)): if word[i-1]==word[i]: count+=1 else :length += word[i-1]+" repeats "+str(count)+", "count=1 length += ("and "+word[i]+" repeats "+str(count))else: i=0 length += ("and "+word[i]+" repeats "+str(count))print (length)输出:
'1 repeats 1, 0 repeats 3, 1 repeats 2, 0 repeats 1, 1 repeats 1, and 0 repeats 1'#'1 repeats 1'



