Вот код, который я написал:
lines = ['add something to this line',
'add nothing to this one',
'emphasize this line',
'emphasize nothing, instead remove a "count" number of characters from the end']
count = 0
new_lines = []
for n,line in enumerate(lines):
if n > 0:
if lines[n-1][:4] == line[:4]:
new_lines.pop(-1)
new_lines.append(lines[n-1] + '!!!')
count += 3
elif n == len(lines)-1:
line = line[:-count]
new_lines.append(line)
new_lines
подходит, но для последней строки. Разве это не должно быть усечено?
['add something to this line!!!',
'add nothing to this one',
'emphasize this line!!!',
'emphasize nothing, instead remove a "count" number of characters from the end']
РЕДАКТИРОВАТЬ: Я хотел написать len(lines)
, а не len(new_lines)