就是itertools.product
这样 - 将每个元素放入a
并将其与 .If 中的所有元素b
配对。如果您希望每个文件中的每一行都配对,那么您想查看itertools.izip
,例如:
from itertools import product, izip
from pprint import pprint
a = ['a_one', 'a_two', 'a_three']
b = ['b_one', 'b_two', 'b_three']
pprint(list(product(a, b)))
[('a_one', 'b_one'),
('a_one', 'b_two'),
('a_one', 'b_three'),
('a_two', 'b_one'),
('a_two', 'b_two'),
('a_two', 'b_three'),
('a_three', 'b_one'),
('a_three', 'b_two'),
('a_three', 'b_three')]
pprint(list(izip(a, b)))
[('a_one', 'b_one'), ('a_two', 'b_two'), ('a_three', 'b_three')]
如果您的目标是比较文件,那么值得查看filecmp和difflib模块。