[英]In Python, how do I iterate over one iterator and then another?
I'd like to iterate two different iterators, something like this: 我想迭代两个不同的迭代器,如下所示:
file1 = open('file1', 'r')
file2 = open('file2', 'r')
for item in one_then_another(file1, file2):
print item
Which I'd expect to print all the lines of file1, then all the lines of file2. 我希望打印file1的所有行,然后是file2的所有行。
I'd like something generic, as the iterators might not be files, this is just an example. 我想要通用的东西,因为迭代器可能不是文件,这只是一个例子。 I know I could do this with: 我知道我可以这样做:
for item in [file1]+[file2]:
but this reads both files into memory, which I'd prefer to avoid. 但这会将两个文件都读入内存,我宁愿避免使用。
Use itertools.chain
: 使用itertools.chain
:
from itertools import chain
for line in chain(file1, file2):
pass
fileinput
module also provides a similar feature: fileinput
模块还提供了类似的功能:
import fileinput
for line in fileinput.input(['file1', 'file2']):
pass
You can also do it with simple generator expression : 您也可以使用简单的生成器表达式来完成 :
for line in (l for f in (file1, file2) for l in f):
# do something with line
with this method you can specify some condition in expression itself: 使用此方法,您可以在表达式中指定一些条件 :
for line in (l for f in (file1, file2) for l in f if 'text' in l):
# do something with line which contains 'text'
The example above is equivalent to this generator with loop: 上面的例子相当于带循环的这个生成器 :
def genlinewithtext(*files):
for file in files:
for line in file:
if 'text' in line:
yield line
for line in genlinewithtext(file1, file2):
# do something with line which contains 'text'
I think the most Pythonic approach to this particular file problem is to use the fileinput
module (since you either need complex context managers or error handling with open
), I'm going to start with Ashwini's example, but add a few things. 我认为这个特定文件问题的最Pythonic方法是使用fileinput
模块(因为你需要复杂的上下文管理器或者open
错误处理),我将从Ashwini的例子开始,但是添加一些东西。 The first is that it's better to open with the U
flag for Universal Newlines support (assuming your Python is compiled with it, and most are), ( r
is default mode, but explicit is better than implicit). 首先,最好使用U
标志打开Universal Newlines支持(假设你的Python是用它编译的,大多数都是),( r
是默认模式,但显式优于隐式)。 If you're working with other people, it's best to support them giving you files in any format. 如果您正在与其他人合作,最好支持他们为您提供任何格式的文件。
import fileinput
for line in fileinput.input(['file1', 'file2'], mode='rU'):
pass
This is also usable on the command line as it will take sys.argv[1:] if you do this: 这也可以在命令行中使用,因为如果你这样做将需要sys.argv [1:]:
import fileinput
for line in fileinput.input(mode='rU'):
pass
And you would pass the files in your shell like this: 你会像下面这样传递shell中的文件:
$ python myscript.py file1 file2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.