简体   繁体   English

Python 如何从组中获取元素的所有组合,每个组中最多有一个元素,并且其中一个组中至少有一个元素

[英]Python how to get all combinations of elements from groups with at most one element from each group and at least one element from one of the groups

I'm working in Python 3.我在 Python 3 工作。

I have five lists like the following:我有五个列表,如下所示:

l1 = [1, 2]
l2 = ['A', 'B', 'C']
l3 = ['d', 'e', 'f', 'g']
l4 = ['word1', 'word2', 'word3', 'word4', 'word5']
l5 = [10, 20, 30, 40, 50, 60]

The lists have variable lengths, and every element in every list is unique.列表具有可变长度,并且每个列表中的每个元素都是唯一的。 I want to find all possible combinations of elements in the lists that meet the following conditions:我想在满足以下条件的列表中找到所有可能的元素组合:

  1. Each combination as at least one element from one of the lists, so no combination is empty.每个组合作为列表之一中的至少一个元素,因此没有组合是空的。 In other words, an empty list [] is not a permitted combination.换句话说,空列表[]不是允许的组合。
  2. Each combination has at most one element from each of the lists, so no combination has multiple elements from the same list.每个组合最多有每个列表中的一个元素,因此没有组合具有来自同一个列表的多个元素。 For example, each of these lists is not permitted as a combination: [1, 'A', 'B'], ['d', 'e'], ['C', 'word2', 'word3'] .例如,这些列表中的每一个都不允许作为组合: [1, 'A', 'B'], ['d', 'e'], ['C', 'word2', 'word3']
  3. Combinations are permitted to have less than one element from every list.允许每个列表中的组合少于一个元素。 For example, each of these lists is individually a permitted combination: [1], [1, 'A'], [1, 'A', 'd'], [1, 'A', 'd', 'word1'], [1, 'A', 'd', 'word1', 10] .例如,这些列表中的每一个都是允许的组合: [1], [1, 'A'], [1, 'A', 'd'], [1, 'A', 'd', 'word1'], [1, 'A', 'd', 'word1', 10]
  4. Order doesn't matter.顺序无所谓。 I'm interested in combinations, not permutations.我对组合感兴趣,而不是排列。 For example, [1, 'A'] is the same combination as ['A', 1] , and [1, 'B', 'word2'] is the same combination as ['B', 'word2', 1] .例如, [1, 'A']['A', 1]的组合相同,而[1, 'B', 'word2']['B', 'word2', 1]组合相同['B', 'word2', 1] .
  5. Combinations shouldn't have duplicates of the same element.组合不应包含相同元素的重复项。 For example, each of these lists is not permitted as a combination: [1, 1, 'A'], ['A', 'd', 'A'] .例如,这些列表中的每一个都不允许作为组合: [1, 1, 'A'], ['A', 'd', 'A']

A consequence of these conditions is that every combination will have at least one element and at most five elements.这些条件的结果是每个组合都至少有一个元素,最多有五个元素。

A solution like the one mentioned here won't work since it doesn't meet condition 3 above.这里提到的那样的解决方案将不起作用,因为它不满足上述条件 3。

I'm looking for an elegant and efficient solution.我正在寻找一个优雅而有效的解决方案。 I'm also looking for a general solution since the actual lists I'm working with differ in size and content from the ones given above.我也在寻找一个通用的解决方案,因为我正在使用的实际列表的大小和内容与上面给出的列表不同。

Add a null element to each list to represent not including any element from that list:将 null 元素添加到每个列表以表示不包括该列表中的任何元素:

all_lists = [l + [None] for l in [l1, l2, l3, l4, l5]]

Now, it's a simple matter to cycle through all the permutations, removing the None elements:现在,循环遍历所有排列,删除None元素是一件简单的事情:

for cross in itertools.product(*all_lists):
    combo = [item for item in cross if item is not None]   # Remove "None" elements
    if combo:    # Ignore the empty list
        print(combo)

Or, to collect them into a list, you can use (nested) comprehension:或者,要将它们收集到一个列表中,您可以使用(嵌套)理解:

result = [combo for combo in [
    [item for item in cross if item is not None]
    for cross in itertools.product(*all_lists)]
        if combo]

You can try this你可以试试这个

l1 = [1, 2]
l2 = ['A', 'B', 'C']
l3 = ['d', 'e', 'f', 'g']
l4 = ['word1', 'word2', 'word3', 'word4', 'word5']
l5 = [10, 20, 30, 40, 50, 60]

import itertools as itt

result = []
for n in range(1, 5):
    for ls in itt.combinations([l1, l2, l3, l4, l5], n):
        result+= list(itt.product(*ls))
        
print(result)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何随机获取 numpy 数组的一定数量的元素,每个 class 至少有一个元素? - How do I randomly get a certain number of elements of a numpy array with at least one element from each class? Python-按一个元素对列表进行分组 - Python - Groups lists by one element 如果我有多个字段,如何从 Dask DataFrameGroupBy 获取所有组? - How to get all groups from Dask DataFrameGroupBy, if I have more then one group by fields? 如何获取列表的所有组合,其中两个相邻的元素可以成为一个元素 - How to get all combinations of a list where two elements next to each other can become one element 从给定的元素列表生成随机的numpy数组,每个元素至少重复一次 - Generate random numpy array from a given list of elements with at least one repetition of each element 在 pyspark 中保留至少一个元素满足条件的组 - Keep groups where at least one element satisfies condition in pyspark 如何从另一个列表中的一个元素中减去列表中的所有元素? - How to subtract all elements in list from one element in another list? 矩阵中第一行的元素,每行取一个元素,并与行的所有元素进行迭代 - elements from first row in a matrix operated with one element taken from each row, iterating with all the elements of rows 如何生成一个假设策略来生成一个列表,该列表至少包含它从中采样的每个元素中的一个? - How can I generate a hypothesis strategy to generate a list that contains at least one of each element it samples from? 来自多个集合的组合,其中每个结果只有每个集合中的一个元素 - Combinations from multiple sets where each result has only one element from each set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM