简体   繁体   English

如何使用 n 个列表的每个列表中的单个元素查找 k 大小元组的所有唯一组合

[英]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 大小的唯一组合,从每个子列表中只选择一个元素。

  • The order of the elements in the combination is not relevant: (a, b) = (b, a)组合中元素的顺序无关紧要:(a, b) = (b, a)

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')
]
  • Extra points for a pythonic way of doing it pythonic 方式的加分
  • Speed/Efficiency matters, the idea is to use in a list with hundreds of lists ranging from 5 to 50 in length速度/效率很重要,这个想法是在一个包含数百个列表的列表中使用,列表的长度从 5 到 50 不等

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)

Try it online! 在线试用!

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.

相关问题 如何在为每个列表选择单个元素时从列表列表中查找 n 个元素的组合 - How to find combination of n elements from list of lists while choosing single element for each list 找出大小至少为 k 到 n 的所有组合 - Find all combinations of at least size k to n 如何通过从唯一列表中最多选择 n 个项目来查找所有组合 - How to find all combinations by picking at most n items from a unique list 如何从列表列表中提取所有唯一组合 - How to pull all the unique combinations from a list of lists 查找每个列表元素的所有组合 - Find all combinations of each individual list element 使大小为k的所有组合从1到数字n - Make all combinations of size k starting from 1 to number n 使用 Python bitset 找到一组大小为 k 的所有组合 - Using Python bitset to find all combinations of a set of size k 获取 m 个列表的 r 长度元组组合,任何列表中不超过一个元素,并且 r &lt; m - Get r-length tuple combinations of m lists, with no more than a single element from any list, and r < m 查找所有组合以将单个列表拆分为两个列表 - Find all combinations to split a single list into two lists 使用递归计算长度为n的列表中的长度k的组合 - Calculating combinations of length k from a list of length n using recursion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM