开发者

pass list of iterables to itertools function

i am using the itertools.product function. i have a 2-deep nested list, which is a list of iterables. i want to pass this to product function dont know how to format it correctly.

to be clear, i want

In [37]: [k for k in product([1,2],['a','b'])]
Out[37]: [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

but generated from the a nested_list input like this

nested_list = [[1,2],['a','b']]

but instead i get

In [36]: [k for k in product(nested_list)]
O开发者_开发技巧ut[36]: [([1, 2],), (['a', 'b'],)]


product takes variable number of arguments, so you need to unpack your list.

list(product(*nested_list)) # without list() normally, of course
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜