Getting the subsets of a set in Python

前端 未结 7 2068
清歌不尽
清歌不尽 2020-12-03 23:19

Suppose we need to write a function that gives the list of all the subsets of a set. The function and the doctest is given below. And we need to complete the whole definitio

7条回答
  •  旧巷少年郎
    2020-12-03 23:35

    >>> from itertools import combinations
    >>> s=set([1,2,3])
    >>> sum(map(lambda r: list(combinations(s, r)), range(1, len(s)+1)), [])
    [(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
    

    produces tuples, but it is close enough for you

提交回复
热议问题