要标记第一个出现的数字及其后的所有行,可以使用以下代码示例:
lines = [
"This is line 1",
"This is line 2",
"This is line 3",
"This is line 4",
"This is line 5 with 123",
"This is line 6 with 456",
"This is line 7 with 789",
"This is line 8"
]
found_digit = False
for line in lines:
if any(char.isdigit() for char in line):
found_digit = True
if found_digit:
print(line)
输出结果为:
This is line 5 with 123
This is line 6 with 456
This is line 7 with 789
This is line 8
这段代码遍历了列表中的每一行,检查是否有数字出现。一旦发现有数字出现,就将found_digit
标记为True
。在之后的每一行中,只要found_digit
为True
,就会输出该行。这样就实现了标记第一个出现的数字及其后的所有行的功能。