嘿所有,我有一些代码来读取文件中的某些行,并想知道它是否会作为列表推导或生成器表达式/函数运行得更快.如果它运行得更快,代码将如何运行?还在学习Python.谢谢你的帮助
input = open('C:/.../list.txt', 'r')
output = open('C:/.../output.txt', 'w')
x=0
for line in input:
x = x+1
if x > 2 and x < 5:
output.write(line)
Run Code Online (Sandbox Code Playgroud)
列表文件有
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
新文件中的输出是
3
4
Run Code Online (Sandbox Code Playgroud)