Python Itertools在两个列表中。从每个列表中获得1个以上的值

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

我有下面的代码。此代码给出了list1和list2之间的所有可能组合。

import itertools
list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]
print(list(itertools.product(list1, list2)))

Output:
[(1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10)]

我想要的是从list1获得每个值的两个可能的组合,从list2获得3个值的所有可能的组合(没有重复项)。因此可能的输出应如下所示。我该怎么办?

[(1,2,6,7,8), (1,2,7,8,9), (1,2,8,9,10), (2,3,6,7,8), and so on.......]
python list permutation itertools
1个回答
0
投票
以下将执行:

from itertools import combinations as c, product as p list1 = [1,2,3,4,5] list2 = [6,7,8,9,10] list(c1 + c2 for c1, c2 in p(c(list1, 2), c(list2, 3)))

© www.soinside.com 2019 - 2024. All rights reserved.