1、为什么使用
做自动化的时候,有这么一个场景,需要将不定量和不定长的数组中的数据进行一个全排列(笛卡尔积)。找了一下资料,发现了itertools.product()方法
2、使用介绍
itertools.product:类似于求多个可迭代对象的笛卡尔积。
使用的形式是:
itertools.product(*iterables, repeat=1),
product(X, repeat=2)等价于product(X, X)。
1)举例
a = [1, 2]
b = [3, 4]
iter_list = itertools.product(a, b)
for item in iter_list:
print(item)
上面的运行结果为
(1, 3)
(1, 4)
(2, 3)
(2, 4)