• <tfoot id='oNS9R'></tfoot>

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

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

        <bdo id='oNS9R'></bdo><ul id='oNS9R'></ul>

      1. 在 Python 中生成所有大小为 k(包含 k 个元素)的子集

        时间:2023-07-03

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

          <i id='DMLb2'><tr id='DMLb2'><dt id='DMLb2'><q id='DMLb2'><span id='DMLb2'><b id='DMLb2'><form id='DMLb2'><ins id='DMLb2'></ins><ul id='DMLb2'></ul><sub id='DMLb2'></sub></form><legend id='DMLb2'></legend><bdo id='DMLb2'><pre id='DMLb2'><center id='DMLb2'></center></pre></bdo></b><th id='DMLb2'></th></span></q></dt></tr></i><div id='DMLb2'><tfoot id='DMLb2'></tfoot><dl id='DMLb2'><fieldset id='DMLb2'></fieldset></dl></div>
          • <bdo id='DMLb2'></bdo><ul id='DMLb2'></ul>
                  <tbody id='DMLb2'></tbody>
                <legend id='DMLb2'><style id='DMLb2'><dir id='DMLb2'><q id='DMLb2'></q></dir></style></legend><tfoot id='DMLb2'></tfoot>
                • 本文介绍了在 Python 中生成所有大小为 k(包含 k 个元素)的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一组值,想创建包含 2 个元素的所有子集的列表.

                  I have a set of values and would like to create list of all subsets containing 2 elements.

                  例如,源集 ([1,2,3]) 具有以下 2 元素子集:

                  For example, a source set ([1,2,3]) has the following 2-element subsets:

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

                  有没有办法在 python 中做到这一点?

                  Is there a way to do this in python?

                  推荐答案

                  好像你想要 itertools.combinations:

                  Seems like you want itertools.combinations:

                  >>> list(itertools.combinations((1, 2, 3), 2))
                  [(1, 2), (1, 3), (2, 3)]
                  

                  如果你想要集合,你必须明确地转换它们.如果您不介意使用迭代而不是列表,并且您使用的是 Python 3,则可以使用 map:

                  If you want sets you'll have to convert them explicitly. If you don't mind an iterable instead of a list, and you're using Python 3, you can use map:

                  >>> s = set((1, 2, 3))
                  >>> map(set, itertools.combinations(s, 2))
                  <map object at 0x10cdc26d8>
                  

                  要一次查看所有结果,您可以将 map 的输出传递给 list.(在 Python 2 中,map 的输出自动是一个列表.)

                  To view all the results at once, you can pass the output of map to list. (In Python 2, the output of map is automatically a list.)

                  >>> list(map(set, itertools.combinations(s, 2)))
                  [{1, 2}, {1, 3}, {2, 3}]
                  

                  但是,如果您知道自己需要一个列表,那么列表理解会稍微好一些(h/t Jacob Bowyer):

                  However, if you know you'll need a list, a list comprehension is marginally better (h/t Jacob Bowyer):

                  >>> [set(i) for i in itertools.combinations(s, 2)]
                  [{1, 2}, {1, 3}, {2, 3}]
                  

                  这篇关于在 Python 中生成所有大小为 k(包含 k 个元素)的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:集合并集查找算法 下一篇:Python 设置的无序是否可以被视为随机序?

                  相关文章

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

                  1. <small id='waOGM'></small><noframes id='waOGM'>

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

                    1. <tfoot id='waOGM'></tfoot>