从多个列表中计算所有可能的组合[重复]

问题描述 投票:0回答:1

我有这些清单:

list_a = set(["A", "B", "C", "D", "E", "F"])
list_b = set(["1", "2", "3", "4", "5", "6"])
list_c = set(["red", "yellow", "blue", "green"])  

我想找到这些列表的可能组合总数(每个列表一个项目)

使用较小的列表很容易实现这一点

import itertools as it

list_set = [list_a, list_b, list_c] 

len(list(it.product(*list_of_unq_vars)))

这将返回组合的数量。

但是对于大型列表,我遇到了内存错误。

有没有办法以这种方式计算可能的组合数而不实际创建组合(如上所述)?

非常感谢,J

python python-3.x itertools
1个回答
5
投票

您需要做的就是将每个列表的长度相乘以获得可能的组合总数:

tempcomb = 1
for l in list_set:
    tempcomb *= len(l)
print(tempcomb)
© www.soinside.com 2019 - 2024. All rights reserved.