【发布时间】:2020-12-19 13:37:29
【问题描述】:
对于我的计算,我需要由 0 和 1 组成的所有 32 元素笛卡尔积,其中有十二个 1。
我目前正在使用以下方法:
for k,l in enumerate(itertools.product([0,1], repeat=32)):
if l.count(1)==12:
# rest code
但正如您所见,对于这么大的笛卡尔积而言,它并不是非常理想的。
如何构建我需要的列表,而无需遍历所有元素 itertools.product 并且没有额外的 if 条件?有没有更快的方法来做到这一点?
【问题讨论】:
标签: python python-3.x itertools cartesian-product