任意数量的Python itertools.product [英] Python itertools.product with arbitrary number of sets

查看:90
本文介绍了任意数量的Python itertools.product的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望执行以下代码:

temp = []
temp.append([1,2])
temp.append([3,4])
temp.append([5,6])

print list(itertools.product(temp[0],temp[1],temp[2]))

但是,我想以任意长度对temp执行它. IE.像这样:

However, I would like to execute it for temp with arbitrary length. I.e. something more like:

print list(itertools.product(temp))

如何正确格式化itertools.product的输入,以在第一段代码中产生相同的结果,而又不明确知道临时有多少个条目?

How do I format the input correctly for itertools.product to produce the same result in the first segment of code without explicitly knowing how many entries there are in temp?

推荐答案

print list(itertools.product(*temp))

使用*将可迭代的参数解压缩为单独的位置参数.

Use * to unpack the argument iterable as separate positional arguments.

这篇关于任意数量的Python itertools.product的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆