How to get all subsets of a set? (powerset)

前端 未结 28 2480
庸人自扰
庸人自扰 2020-11-22 05:18

Given a set

{0, 1, 2, 3}

How can I produce the subsets:

[set(),
 {0},
 {1},
 {2},
 {3},
 {0, 1},
 {0, 2},
 {0, 3},
 {1, 2}         


        
28条回答
  •  盖世英雄少女心
    2020-11-22 05:48

    Here is more code for a powerset. This is written from scratch:

    >>> def powerset(s):
    ...     x = len(s)
    ...     for i in range(1 << x):
    ...         print [s[j] for j in range(x) if (i & (1 << j))]
    ...
    >>> powerset([4,5,6])
    []
    [4]
    [5]
    [4, 5]
    [6]
    [4, 6]
    [5, 6]
    [4, 5, 6]
    

    Mark Rushakoff's comment is applicable here: "If you don't like that empty tuple at the beginning, on."you can just change the range statement to range(1, len(s)+1) to avoid a 0-length combination", except in my case you change for i in range(1 << x) to for i in range(1, 1 << x).


    Returning to this years later, I'd now write it like this:

    def powerset(s):
        x = len(s)
        masks = [1 << i for i in range(x)]
        for i in range(1 << x):
            yield [ss for mask, ss in zip(masks, s) if i & mask]
    

    And then the test code would look like this, say:

    print(list(powerset([4, 5, 6])))
    

    Using yield means that you do not need to calculate all results in a single piece of memory. Precalculating the masks outside the main loop is assumed to be a worthwhile optimization.

提交回复
热议问题