[英]How to find all unique combinations of k size tuple using a single element from each list of n lists
Given a list containing N sublists of multiple lengths, find all unique combinations of ak size, selecting only one element from each sublist.给定一个包含 N 个多个长度的子列表的列表,找到所有 k 大小的唯一组合,从每个子列表中只选择一个元素。
sample_k = 2
sample_list = [['B1','B2','B3'], ['T1','T2'], ['L1','L2','L3','L4']]
expected_output =
[
('B1', 'T1'),('B1', 'T2'),('B1', 'L1'),('B1', 'L2'),('B1', 'L3'),('B1', 'L4'),
('B2', 'T1'),('B2', 'T2'),('B2', 'L1'),('B2', 'L2'),('B2', 'L3'),('B2', 'L4'),
('B3', 'T1'),('B3', 'T2'),('B3', 'L1'),('B3', 'L2'),('B3', 'L3'),('B3', 'L4'),
('T1', 'L1'),('T1', 'L2'),('T1', 'L3'),('T1', 'L4'),
('T2', 'L1'),('T2', 'L2'),('T2', 'L3'),('T2', 'L4')
]
What I have been able to accomplish so far: Using for and while loops to move pointers and build the answer, however I am having a hard time figuring out how to include K parameter to set the size of tuple combination dinamically.到目前为止我已经能够完成的事情:使用 for 和 while 循环移动指针并构建答案,但是我很难弄清楚如何包含 K 参数来动态设置元组组合的大小。 (not really happy about it) (真的不是很开心)
def build_combinations(lst):
result = []
count_of_lst = len(lst)
for i, sublist in enumerate(lst):
if i == count_of_lst - 1:
continue
else:
for item in sublist:
j = 0
while i < len(lst)-1:
while j <= len(lst[i+1])-1:
comb = (item, lst[i+1][j])
result.append(comb)
j = j + 1
i = i + 1
j = 0
i = 0
return result
I've seen many similar questions in stack overflow, but none of them addressed the parameters the way I am trying to (one item from each list, and the size of the combinations being a params of function)我在堆栈溢出中看到过许多类似的问题,但没有一个以我尝试的方式解决参数(每个列表中的一个项目,组合的大小是函数的参数)
I tried using itertools combinations, product, permutation and flipping them around without success.我尝试使用 itertools 组合、乘积、排列并翻转它们,但没有成功。 Whenever using itertools I have either a hard time using only one item from each list, or not being able to set the size of the tuple I need.每当使用 itertools 时,我要么很难只使用每个列表中的一个项目,要么无法设置我需要的元组的大小。
I tried NumPy using arrays and a more math/matrix approach, but didn't go too far.我尝试了 NumPy 使用 arrays 和更多的数学/矩阵方法,但没有 go 太远。 There's definitely a way of solving with NumPy, hence why I tagged numpy as well肯定有解决 NumPy 的方法,因此我也标记了 numpy
You need to combine two itertools
helpers, combinations
to select the two unique ordered list
s to use, then product
to combine the elements of the two:您需要组合两个itertools
助手, combinations
到 select 两个唯一的有序list
以使用,然后product
以组合两者的元素:
from itertools import combinations, product
sample_k = 2
sample_list = [['B1','B2','B3'], ['T1','T2'], ['L1','L2','L3','L4']]
expected_output = [pair
for lists in combinations(sample_list, sample_k)
for pair in product(*lists)]
print(expected_output)
If you want to get really fancy/clever/ugly, you can push all the work down to the C layer with:如果你真的想变得花哨/聪明/丑陋,你可以将所有工作推到 C 层:
from itertools import combinations, product, starmap, chain
sample_k = 2
sample_list = [['B1','B2','B3'], ['T1','T2'], ['L1','L2','L3','L4']]
expected_output = list(chain.from_iterable(starmap(product, combinations(sample_list, sample_k))))
print(expected_output)
That will almost certainly run meaningfully faster for huge inputs (especially if you can loop the results from chain.from_iterable
directly rather than realizing them as a list
), but it's probably not worth the ugliness unless you're really tight for cycles (I wouldn't expect much more than a 10% speed-up, but you'd need to benchmark to be sure).对于巨大的输入,这几乎肯定会运行得更快(特别是如果你可以直接从chain.from_iterable
循环结果而不是将它们实现为list
),但它可能不值得丑陋,除非你真的很紧张周期(我会期望速度提高 10% 以上,但您需要进行基准测试才能确定)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.