<legend id='prrHv'><style id='prrHv'><dir id='prrHv'><q id='prrHv'></q></dir></style></legend>

<small id='prrHv'></small><noframes id='prrHv'>

    • <bdo id='prrHv'></bdo><ul id='prrHv'></ul>

    1. <tfoot id='prrHv'></tfoot>
      <i id='prrHv'><tr id='prrHv'><dt id='prrHv'><q id='prrHv'><span id='prrHv'><b id='prrHv'><form id='prrHv'><ins id='prrHv'></ins><ul id='prrHv'></ul><sub id='prrHv'></sub></form><legend id='prrHv'></legend><bdo id='prrHv'><pre id='prrHv'><center id='prrHv'></center></pre></bdo></b><th id='prrHv'></th></span></q></dt></tr></i><div id='prrHv'><tfoot id='prrHv'></tfoot><dl id='prrHv'><fieldset id='prrHv'></fieldset></dl></div>

      1. Python:带有生成器的给定集合的幂集

        时间:2023-07-03
        <tfoot id='2uDkH'></tfoot>

          <i id='2uDkH'><tr id='2uDkH'><dt id='2uDkH'><q id='2uDkH'><span id='2uDkH'><b id='2uDkH'><form id='2uDkH'><ins id='2uDkH'></ins><ul id='2uDkH'></ul><sub id='2uDkH'></sub></form><legend id='2uDkH'></legend><bdo id='2uDkH'><pre id='2uDkH'><center id='2uDkH'></center></pre></bdo></b><th id='2uDkH'></th></span></q></dt></tr></i><div id='2uDkH'><tfoot id='2uDkH'></tfoot><dl id='2uDkH'><fieldset id='2uDkH'></fieldset></dl></div>

            <small id='2uDkH'></small><noframes id='2uDkH'>

              • <bdo id='2uDkH'></bdo><ul id='2uDkH'></ul>
                    <tbody id='2uDkH'></tbody>
                1. <legend id='2uDkH'><style id='2uDkH'><dir id='2uDkH'><q id='2uDkH'></q></dir></style></legend>
                  本文介绍了Python:带有生成器的给定集合的幂集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 generators 在 Python 中构建给定集合的子集列表.说我有

                  I am trying to build a list of subsets of a given set in Python with generators. Say I have

                  set([1, 2, 3])

                  作为输入,我应该有

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

                  作为输出.我怎样才能做到这一点?

                  as output. How can I achieve this?

                  推荐答案

                  最快的方法是使用itertools,尤其是chain和combinations:

                  The fastest way is by using itertools, especially chain and combinations:

                  >>> 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 并将元组转换为集合:

                  If you need a generator just use yield and turn tuples into sets:

                  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])
                  >>> 
                  

                  这篇关于Python:带有生成器的给定集合的幂集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在python中的List of Lists中获取唯一值 下一篇:Python 和 Numpy 的 nan 和 set

                  相关文章

                2. <small id='0CM72'></small><noframes id='0CM72'>

                  <tfoot id='0CM72'></tfoot>

                      <legend id='0CM72'><style id='0CM72'><dir id='0CM72'><q id='0CM72'></q></dir></style></legend>

                      <i id='0CM72'><tr id='0CM72'><dt id='0CM72'><q id='0CM72'><span id='0CM72'><b id='0CM72'><form id='0CM72'><ins id='0CM72'></ins><ul id='0CM72'></ul><sub id='0CM72'></sub></form><legend id='0CM72'></legend><bdo id='0CM72'><pre id='0CM72'><center id='0CM72'></center></pre></bdo></b><th id='0CM72'></th></span></q></dt></tr></i><div id='0CM72'><tfoot id='0CM72'></tfoot><dl id='0CM72'><fieldset id='0CM72'></fieldset></dl></div>
                        <bdo id='0CM72'></bdo><ul id='0CM72'></ul>