简体   繁体   English

在列表中找到2 ^ n -2个元素的组合

[英]find 2^n -2 combinations of elements in a list

I have the following list: 我有以下清单:

list1 = ['g1','g2','g3','g4']

I want to find 2^n-2 combinations where n is the total number of items in the list. 我想找到2^n-2组合,其中n是列表中项目的总数。 For n = 4 the result should be 2^4 -2 = 14 , ie 14 combinations. 对于n = 4 ,结果应为2^4 -2 = 14 ,即14个组合。

The combinations are: 组合为:

[[['g1'],['g2','g3','g4']],[['g2'],['g1','g3','g4']], [['g3'],['g1','g2','g4']],['g4'],['g1','g2','g3']],[['g1','g2'],['g3','g4']],[['g1','g3'],['g2','g4']],[['g1','g4'],['g3','g4']],[['g2','g3'],['g1','g4']],
[['g2','g4'],['g1','g3']],[['g3','g4'],['g1','g2']],[['g1','g2','g3'],['g4']],[['g2','g3','g4'],['g1']],[['g3','g4','g1'],['g2']],[['g4','g1','g2'],['g3']]]

I know one approach: in first iteration take single element and put it into a list and other elements in second list: ['g1'],['g2','g3','g4'] in second iteration take 2 elements in a list and other elements in second list. 我知道一种方法:在第一次迭代中,将单个元素放入列表,然后将其放入第二个列表中的其他元素: ['g1'],['g2','g3','g4']在第二次迭代中使用2个元素一个列表和第二个列表中的其他元素。 ['g1','g2'],['g1','g4'] Is there any other approach ?? ['g1','g2'],['g1','g4']还有其他方法吗? I'm writing this program in python. 我正在用python编写此程序。 My approach is costly. 我的方法很昂贵。 Is there any library method to do this quickly. 是否有任何库方法可以快速执行此操作。

Here's a functional approach using itertools 这是使用itertools的功能方法

import itertools as iter

list1 = ['g1', 'g2', 'g3', 'g4']
combinations = [iter.combinations(list1, n) for n in range(1, len(list1))]
flat_combinations = iter.chain.from_iterable(combinations)
result = map(lambda x: [list(x), list(set(list1) - set(x))], flat_combinations)
# [[['g1'], ['g4', 'g3', 'g2']], [['g2'], ['g4', 'g3', 'g1']], [['g3'], ['g4', 'g2', 'g1']],...
len(result)
# 14

itertools.combinations(iterable, r) itertools.combinations(iterable,r)

Return r length subsequences of elements from the input iterable. 从可迭代的输入中返回元素的r长度子序列。 Combinations are emitted in lexicographic sort order. 组合按字典顺序排序。 So, if the input iterable is sorted, the combination tuples will be produced in sorted order. 因此,如果对输入的iterable进行排序,则将按排序顺序生成组合元组。

from itertools import combinations
list1 = ['g1','g2','g3','g4']
for n in range(1,len(list1)):
    for i in combinations(list1,n):
        print(set(i), set(list1) - set(i))

out: 出:

{'g1'} {'g2', 'g3', 'g4'}
{'g2'} {'g1', 'g3', 'g4'}
{'g3'} {'g1', 'g2', 'g4'}
{'g4'} {'g1', 'g2', 'g3'}
{'g1', 'g2'} {'g3', 'g4'}
{'g1', 'g3'} {'g2', 'g4'}
{'g1', 'g4'} {'g2', 'g3'}
{'g2', 'g3'} {'g1', 'g4'}
{'g2', 'g4'} {'g1', 'g3'}
{'g3', 'g4'} {'g1', 'g2'}
{'g1', 'g2', 'g3'} {'g4'}
{'g1', 'g2', 'g4'} {'g3'}
{'g1', 'g3', 'g4'} {'g2'}
{'g2', 'g3', 'g4'} {'g1'}

you can try this 你可以试试这个

I like the solution from the Chinese coder (I guess). 我喜欢中文编码器的解决方案(我想)。 Here's my own solution: 这是我自己的解决方案:

import itertools


def flatten(*z):
    return z


list1 = ['g1','g2','g3','g4']

sublists = []
for i in range(1, len(list1)):
    sublists.extend(itertools.combinations(list1, i))

pairs = []
for a, b in itertools.product(sublists, sublists):
    if len(a) + len(b) == len(list1) and \
    len(set(flatten(*a, *b))) == len(list1):
        pairs.append((a, b))

print(pairs, len(pairs))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM