Python 双重迭代

19 投票
3 回答
21515 浏览
提问于 2025-04-15 20:05

如何同时遍历两个列表的pythonic方式是什么?

假设我想逐行比较两个文件(也就是说,把一个文件的第i行和另一个文件的第i行进行比较),我想做的事情大概是这样的:

file1 = csv.reader(open(filename1),...)
file2 = csv.reader(open(filename2),...)

for line1 in file1 and line2 in file2: #pseudo-code!
    if line1 != line2:
        print "files are not identical"
        break

那有什么pythonic的方法可以做到这一点呢?


编辑:我不是在使用文件处理器,而是用CSV读取器(csv.reader(open(file),...)),而且zip()似乎对它不起作用……


最后编辑:就像@Alex M.提到的,zip()在第一次遍历时会把文件加载到内存里,所以对于大文件来说,这会是个问题。在Python 2中,使用itertools可以解决这个问题。

3 个回答

4

在同步执行(适用于Python 3及以上版本):

for line1, line2 in zip(file1, file2):
   # etc.

作为一个“二维数组”:

for line1 in file1:
   for line2 in file2:
     # etc.
   # you may need to rewind file2 to the beginning.
12

我推荐使用 zip。手册上提到“要同时遍历两个或多个序列,可以使用 zip() 函数将它们配对在一起。”

比如说,

list_one = ['nachos', 'sandwich', 'name']
list_two = ['nachos', 'sandwich', 'the game']
for one, two in zip(list_one, list_two):
   if one != two:
      print "Difference found"
16

在Python 2中,你需要导入itertools模块,并使用它的izip函数:

with open(file1) as f1:
  with open(file2) as f2:
    for line1, line2 in itertools.izip(f1, f2):
      if line1 != line2:
        print 'files are different'
        break

如果你使用内置的zip函数,那么在循环开始时,两个文件会一次性全部读入内存,这可能不是你想要的效果。在Python 3中,内置的zip函数的工作方式就像Python 2中的itertools.izip,是逐步读取的。

撰写回答