【发布时间】:2020-08-05 21:30:58
【问题描述】:
这是我写的代码:
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)
【问题讨论】:
标签: python list loops for-loop indexing