Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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:具有生成器的给定集的powerset_Python_Algorithm_Set_Generator_Powerset - Fatal编程技术网

Python:具有生成器的给定集的powerset

Python:具有生成器的给定集的powerset,python,algorithm,set,generator,powerset,Python,Algorithm,Set,Generator,Powerset,我试图用Python中的生成器构建给定集合的子集列表。说我有 set([1,2,3]) 作为输入,我应该 [set([1,2,3])、set([2,3])、set([1,3])、set([3])、set([1,2])、set([2])、set([1])、set([])] 作为输出。如何实现这一点?来自itertools文档的一节: def powerset(iterable): s = list(iterable) return chain.from_iterable(combi

我试图用Python中的生成器构建给定集合的子集列表。说我有

set([1,2,3])

作为输入,我应该

[set([1,2,3])、set([2,3])、set([1,3])、set([3])、set([1,2])、set([2])、set([1])、set([])]

作为输出。如何实现这一点?

来自itertools文档的一节:

def powerset(iterable):
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

最快的方法是使用itertools,尤其是链和组合:

>>> from itertools import chain, combinations
>>> i = set([1, 2, 3])
>>> for z in chain.from_iterable(combinations(i, r) for r in range(len(i)+1)):
    print z 
()
(1,)
(2,)
(3,)
(1, 2)
(1, 3)
(2, 3)
(1, 2, 3)
>>> 
如果需要生成器,只需使用yield并将元组转换为集合:

def powerset_generator(i):
    for subset in chain.from_iterable(combinations(i, r) for r in range(len(i)+1)):
        yield set(subset)
然后简单地说:

>>> for i in powerset_generator(i):
    print i


set([])
set([1])
set([2])
set([3])
set([1, 2])
set([1, 3])
set([2, 3])
set([1, 2, 3])
>>> 

我知道这太老了,但我一直在寻找同一个问题的答案,在几个小时的不成功的网络搜索后,我想出了自己的解决方案。代码如下:

def combinations(iterable, r):
    # combinations('ABCDE', 3) --> ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE
    pool = tuple(iterable)  # allows a string to be transformed to a tuple
    n = len(pool)  
    if r > n:  # If we don't have enough items to combine, return None
        return
    indices = range(r)  # Make a set of the indices with length (r)
    yield [pool[i] for i in indices]   Yield first list of indices [0 to (r-1)]
    while True:
        for i in reversed(range(r)):  # Check from right to left if the index is at its
                                      # max value. If everyone is maxed out, then finish
            if indices[i] != i + n - r:  # indices[2] != 2 + 5 - 3
                break                    # 2 != 4  (Y) then break and avoid the return
        else:
            return
        indices[i] += 1  # indices[2] = 2 + 1 = 3
        for j in range(i + 1, r):  # for j in []  # Do nothing in this case
            indices[j] = indices[j - 1] + 1  # If necessary, reset indices to the right of
                                             # indices[i] to the minimum value possible.
                                             # This depends on the current indices[i]
        yield [pool[i] for i in indices]  # [0, 1, 3]


def all_subsets(test):
    out = []
    for i in xrange(len(test)):
        out += [[test[i]]]
    for i in xrange(2, len(test) + 1):
        out += [x for x in combinations(test, i)]
    return out

我从itertools文档中获取了组合示例代码,并对其进行了修改,以生成列表而不是元组。当我试图弄清楚它是如何工作的(以便以后修改它)时,我做了注释,所以我会把它们放在那里,以防万一有人发现它们有用。最后,我使用all_substes函数来查找从长度1到r的每个子集(不包括空列表,因此如果需要它,只需从
out=[[]开始即可)

谷歌搜索:
python itertools powerset recipe
?这正是你想要的…然后继续搜索
powerset
…这对我没有帮助,请检查下面的回复。因为输入是一个集合,所以输出不能包含重复的元素,所以元组没有任何优势,如果你需要,请将其转换回集合ally want。另外,由于它返回
chain.from\u iterable
实际上您有一个生成器。有什么东西您不能轻松适应您的需求?
返回imap(set,chain.from\u iterable(…)
?我需要为此构建一个生成器,一个生成所需子集的函数。此外,生成器必须包含集合,而不是元组。我需要为此构建一个生成器,一个生成所需子集的函数。此外,生成器必须包含集合,而不是元组。@Pawelmhm或根据我在OP上的评论-只需将其包装在
imap中即可(设置,…)
并保持功能完全相同。。。