2024-04-25 19:49:49 发布
网友
下面是一个示例代码
with open('somefile.txt', 'r') as file: for line in file: find = 'some string' if find in line: if previous line is the same as this line: #how to write this ? pass else: print(line)
如何在这段代码中写第5行?你知道吗
在每次迭代时保存前一行:
with open('test.txt', 'r') as file: previous = None find = 'n' for line in file: if find in line: if line != previous: print(line) previous = line
样本输入:
hello no no hello
输出:
no
在每次迭代时保存前一行:
样本输入:
输出:
相关问题 更多 >
编程相关推荐