打印所有组合,python

时间:2012-08-15 11:51:44

标签: python combinations

说我有3个不同的变量,每个变量有2个可能的值,所以我总共有8个不同的组合。是否有python库函数,或者我可以用来打印所有可能组合的算法?

由于

2 个答案:

答案 0 :(得分:11)

我认为您正在寻找product

a = [1, 2]
b = [100, 200]
c = [1000, 2000]

import itertools
for p in itertools.product(a, b, c):
    print p

打印:

(1, 100, 1000)
(1, 100, 2000)
(1, 200, 1000)
(1, 200, 2000)
(2, 100, 1000)
(2, 100, 2000)
(2, 200, 1000)
(2, 200, 2000)

答案 1 :(得分:1)

这里真正的一个功能是itertools.product