Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表值上创建组合_Python_Grouping_Combinations - Fatal编程技术网

Python 在列表值上创建组合

Python 在列表值上创建组合,python,grouping,combinations,Python,Grouping,Combinations,我有一个要求,比如说我有一份清单 lis=['a','b','c','d','e','f'] 我现在必须创建它们的组合,例如: l1=[a],'b,c,d,e,f] l2:[b],[a、c、d、e、f] l10[a,b,c],[d,e,f] l11[a,b,c,d][e,f] 将删除左侧和右侧节点上的重复元素: 例如:我不需要两个列表: l1:[b,c],[a,d,e,f] l2:[a,d,e,f],[b,c] 因为它们是一样的 我想到的伪代码是: 对于length=1,我将从列表中选取一

我有一个要求,比如说我有一份清单 lis=['a','b','c','d','e','f'] 我现在必须创建它们的组合,例如:

l1=[a],'b,c,d,e,f]

l2:[b],[a、c、d、e、f]

l10[a,b,c],[d,e,f]

l11[a,b,c,d][e,f]

将删除左侧和右侧节点上的重复元素: 例如:我不需要两个列表:

l1:[b,c],[a,d,e,f]

l2:[a,d,e,f],[b,c]

因为它们是一样的

我想到的伪代码是: 对于length=1,我将从列表中选取一个元素,然后合并其他元素

长度为2时类似,将使用两个元素,并使用其他元素

直到长度=len(列表)-1,将进行杆式起下钻

然后再删除重复项


我可以尝试更好的解决方案吗?

这可能不是最佳方案,但非常简单:

from itertools import chain, combinations


def power_set(iterable):
    """power_set([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"""
    source = list(iterable)
    return chain.from_iterable(combinations(source, r) for r in range(1, len(source) // 2 + 1))


def complement(source, universe):
    return tuple(set(universe) - set(source))


lst = ['a', 'b', 'c', 'd', 'e', 'f']

result = set(frozenset({si, complement(si, lst)}) for si in power_set(lst))

for s, c in result:
    print(s, c)
输出

('a', 'd', 'e') ('f', 'c', 'b')
('f', 'a', 'c', 'b') ('d', 'e')
('b', 'e') ('d', 'f', 'a', 'c')
('a', 'b', 'f') ('d', 'e', 'c')
('e', 'd', 'a', 'f', 'b') ('c',)
('c', 'f') ('d', 'a', 'e', 'b')
('d', 'f') ('a', 'e', 'b', 'c')
('d',) ('e', 'c', 'a', 'f', 'b')
('f', 'a', 'e', 'c') ('b', 'd')
('e', 'c', 'd', 'a', 'b') ('f',)
('b', 'c', 'd') ('f', 'a', 'e')
('a', 'b', 'e') ('d', 'f', 'c')
('b', 'c') ('d', 'f', 'a', 'e')
('f', 'a', 'b') ('c', 'd', 'e')
('d', 'e', 'b', 'c') ('a', 'f')
('c', 'd', 'f') ('a', 'e', 'b')
('e', 'c', 'd', 'f', 'b') ('a',)
('a', 'c') ('d', 'f', 'e', 'b')
('f', 'e', 'c') ('a', 'b', 'd')
('a', 'd') ('f', 'e', 'b', 'c')
('b', 'c', 'e') ('d', 'f', 'a')
('a', 'c', 'e') ('d', 'f', 'b')
('d', 'e', 'f') ('a', 'c', 'b')
('a', 'c', 'd') ('f', 'e', 'b')
('d', 'f', 'e', 'c') ('a', 'b')
('f', 'a', 'e', 'b') ('c', 'd')
('d', 'a', 'c') ('b', 'e', 'f')
('a', 'e') ('d', 'f', 'c', 'b')
('a', 'b', 'c') ('d', 'f', 'e')
('a', 'd', 'f') ('e', 'b', 'c')
('d', 'e', 'b') ('a', 'c', 'f')
('c', 'd', 'a', 'f', 'b') ('e',)
('b',) ('e', 'c', 'd', 'a', 'f')
('e', 'f') ('d', 'a', 'c', 'b')
('d', 'c', 'b') ('a', 'e', 'f')
('b', 'f') ('d', 'a', 'e', 'c')
('d', 'a', 'e') ('b', 'c', 'f')
('b', 'd', 'e') ('f', 'a', 'c')
('a', 'e', 'c') ('b', 'd', 'f')
('c', 'e') ('d', 'f', 'a', 'b')
('d', 'a', 'b') ('c', 'e', 'f')

你想要动力装置吗@我想不止这些。似乎对于每个子集,它都需要与其互补项相关联。您只需转到
len(list)//2
。而且可能值得使用一个集合,这样就不会出现重复。但是你必须使用散列类型。列表中可能有重复的元素吗?重复的元素不是必需的,所以最终我将创建一个树,这些组合将只是左和右根节点。因此,重复并不重要,谢谢你的帮助。但是你知道如何消除重复的吗?例如:一个结果是('b','c')和('a','d','e','f'),另一个结果是('c','b')和('a','d','e','f')只具有
范围(1,len(s)//2+1)
,这将有助于处理一些重复项,并且会使事情变得更复杂faster@AkshatShreemali我已经用MZ的建议更新了解决方案,看看它是否工作正常