我想使用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)
答案 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)
相同。这称为参数解压缩。