简体   繁体   English

来自多个集合的组合,其中每个结果只有每个集合中的一个元素

[英]Combinations from multiple sets where each result has only one element from each set

So I have some sets:所以我有一些套装:

sets = [
   {a1,a2}, #a set
   {b1,b2}, #b set
   {c1,c2}, #c set
   ...
]

And I need to get all possible combinations of all sizes... but only one element from each set.而且我需要获得所有尺寸的所有可能组合......但每组只有一个元素。

[
   {a1}, {b1}, ...,                        # all single
   {a1, b1}, {a1, b2} ...,                 # all pairs
   ...                                     # all combinations less than n
   {a1,b1,c1 ... }, {a2,b1,c1 ... }, ...,  # size n
]

At the moment I am using iter_tools' powerset on the union of all the sets then filtering, but this is incredibly slow when the sets get large.目前我在所有集合的并集上使用 iter_tools 的 powerset 然后过滤,但是当集合变大时,这非常慢。

large_set = union_all_sets(sets)
r = more_itertools.powerset(large_set)
r = filter_1(r)

I was wondering if there is a simple function for this that I don't know the name of?我想知道是否有一个我不知道名字的简单 function ? Or if anybody had any suggestions how I can avoid calling powerset on the large set?或者,如果有人对我如何避免在大型设备上调用 powerset 有任何建议?

You can build combinations of 1, then 2... n of your sets, and for each of these combinations, generate the cartesian product:您可以构建 1,然后 2... n 的集合的组合,并且对于这些组合中的每一个,生成笛卡尔积:

from itertools import product, combinations

sets = [
   {"a1", "a2"}, #a set
   {"b1", "b2"}, #b set
   {"c1", "c2", "c3"} #c set
]

out = []

for size in range(1, len(sets)+1):
    for combination in combinations(sets, r=size):
        out.extend(set(p) for p in product(*combination))

print(out)

Output: Output:

[{'a2'}, {'a1'}, {'b1'}, {'b2'}, {'c2'}, {'c1'}, {'c3'}, 
 {'b1', 'a2'}, {'b2', 'a2'}, {'b1', 'a1'}, {'b2', 'a1'}, {'c2', 'a2'},
 {'c1', 'a2'}, {'c3', 'a2'}, {'c2', 'a1'}, {'c1', 'a1'}, {'c3', 'a1'}, 
 {'b1', 'c2'}, {'b1', 'c1'}, {'b1', 'c3'}, {'c2', 'b2'}, {'c1', 'b2'}, 
 {'b2', 'c3'}, 
 {'b1', 'c2', 'a2'}, {'b1', 'c1', 'a2'}, {'b1', 'c3', 'a2'}, {'c2', 'b2', 'a2'}, 
 {'c1', 'b2', 'a2'}, {'b2', 'c3', 'a2'}, {'b1', 'c2', 'a1'}, {'b1', 'c1', 'a1'}, 
 {'b1', 'c3', 'a1'}, {'c2', 'b2', 'a1'}, {'c1', 'b2', 'a1'}, {'b2', 'c3', 'a1'}]

暂无
暂无

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

相关问题 将项目列表转换为多个字典,其中每个键只有列表中的一个值 - Turn a list of items into multiple dictionaries where each key has only one value from the list 从时间间隔列表中查找所有时间间隔集,其中一个集合中的每个EACH时间间隔与该集合中的所有时间间隔重叠 - From a List of Intervals, Finding all Sets of Intervals where EACH Interval in One Set Overlaps with All Intervals in that Set 查找从不同集合中选取每个数字的所有组合 - Finding all combinations where each digit is picked from a different set 从多个列表创建字典,其中每个列表没有一个键的值,但每个列表都有所有键的值 - create dictionary from multiple lists, where each list has not the values for one key, but each list has values for all keys 如何在python中将一个包含5个元素的集合变成5个集合,每个集合一个元素? - how to turn one set with 5 elements into 5 sets each with one element in python? 从 N 个桶中生成所有项目组合的算法,其中每个桶只能有 1 个项目 - Algorithm to make all combinations of items from N buckets where only 1 item can come from each bucket 创建一个列表,其中每个元素只有一次来自 Python 中其他两个列表的元素 - Create a list which has only one time each element from two other lists in Python 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 从由多个列表组成的字典中,从每个列表中选择一个元素 - From a dictionary made of multiple lists choose one element from each arrays 的所有可能组合,其中数组的每个元素都有 4 个可能的数字,python - All possible combinations of arrays where each element of the array has 4 possible numbers, python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM