Продолжить цикл с начала
Как начать/продолжить цикл(следующие значения i,j). “Input=AaaassDffReerh”
a,new=list(input()),[]
t=1
for i in a:
for j in a[1:]:
if i==j:
while i==j:
t+=1
Ответы (3 шт):
Автор решения: Kers
→ Ссылка
В какой-то момент Ваш цикл while становится бесконечным while True.
a,new=list("AaaassDffReerh"),[]
print(a[1:])
t=1
for i in a:
for j in a[1:]:
if i==j:
while i==j:
t+=1
break # позволит прервать цикл
P.S. Опишите Вашу задачу. Есть мысль это можно сделать немного поудачнее.
Через словарь:
a = "AaaassDffReerh"
dic = {}
for i in a:
if i not in dic:
dic[i] = 1 # создаем новый ключ
else:
dic[i] += 1 # увеличиваем значение на 1
s = []
for i in dic:
if dic.get(i) <= 1:
s+= i
else:
s+=(str(dic.get(i)) + i)
print(''.join(s))
Автор решения: Виталий
→ Ссылка
a,new,t,u=list(input()),[],1,0
while u<len(a):
if u+1!=len(a) and a[u]==a[u+1]:
while a[u]==a[u+1]:
u+=1
t+=1
if u+1==len(a):
break
new.append(t)
new.append(a[u])
t=1
else:
new.append(a[u])
u+=1
print(*new,sep="")
Автор решения: FatCatStudent
→ Ссылка
Так должно работать?
text = list(input())
text += chr(ord(text[len(text) - 1]) + 1)
result = ''
now_char = text[0]
count = 1
for i in range(1, len(text)):
if text[i] == now_char:
count += 1
else:
if count == 1:
result += now_char
else:
result += str(count) + now_char
now_char = text[i]
count = 1
print(result)