How to iterate through 2 lists at once?
本问题已经有最佳答案,请猛点这里访问。
1 2 3 | with open('myfile.txt') as f: for line in f: doSomething(line) |
有一种方法可以同时迭代两个文件吗?这里有一些伪代码可以帮助您理解……
1 2 3 | with open('myfile.txt') as f: and with open ('myfile2.txt') as d: for line in f and for line2 in d: doSomething(line, line2) |
1 2 3 | with open('myfile.txt') as f, open('myfile2.txt') as d: for line1, line2 in zip(f, d): do_something(line1, line2) |
注意,对于python 2,应该使用