2 回答

TA贡献1801条经验 获得超8个赞
以下操作即可:
from itertools import combinations as com, product as prod
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
[c1 + c2 for c1, c2 in prod(com(list1, 2), com(list2, 3))]
# [(1, 2, 6, 7, 8),
# (1, 2, 6, 7, 9),
# (1, 2, 6, 7, 10),
# ...
# (4, 5, 7, 9, 10),
# (4, 5, 8, 9, 10)]
这使得两个列表中的相应组合的笛卡尔积,并简单地连接每对以避免嵌套元组。

TA贡献2065条经验 获得超14个赞
你需要先构建每个列表需要的组合,然后做产品,你还需要加入产品的内部结果((1, 2), (6, 7, 8)) => (1, 2, 6, 7, 8)
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
c1 = combinations(list1, r=2)
c2 = combinations(list2, r=3)
print(list(map(lambda x: tuple(chain(*x)), product(c1, c2)))) # [(1, 2, 6, 7, 8), (1, 2, 6, 7, 9), (1, 2, 6, 7, 10), (1, 2, 6, 8, 9), (1, 2, 6, 8, 10), (1, 2, 6, 9, 10), (1, 2, 7
添加回答
举报