itertools.product参数的类型?

时间:2019-01-15 20:54:04

标签: python itertools

我想使用python itertools.product()。它需要什么类型的输入?我只想输入1个变量。应该如何构造?

a = [1,2,3,4]
b = [5,6,7,8]
itertools.product(a,b) # this works.

是否只能传递1个参数?例如:

c = (a,b)
itertools.product(c)

1 个答案:

答案 0 :(得分:1)

itertools.chain

比较
a = [1,2,3,4]
b = [5,6,7,8]
c = [a, b]

itertools.chain(a, b)             # 1 2 3 4 5 6 7 8
itertools.chain(c)                # [1, 2, 3, 4] [5, 6, 7, 8]

itertools.chain(*c)               # 1 2 3 4 5 6 7 8
# or chain specifically has a more-legible version of this
itertools.chain.from_iterable(c)  # 1 2 3 4 5 6 7 8

请注意,some_function(*[a, b, c])some_function(a, b, c)相同。这称为参数解压缩。