您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python双迭代

Python双迭代

在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中,内置功能与Python 2中的功能zip类似itertools.izip-逐渐递增。

python 2022/1/1 18:49:35 有356人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶