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

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

      1. <legend id='BRnxc'><style id='BRnxc'><dir id='BRnxc'><q id='BRnxc'></q></dir></style></legend>

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

        python中'set.intersection()'的算法是什么?

        时间:2023-07-03
        <legend id='PcrdQ'><style id='PcrdQ'><dir id='PcrdQ'><q id='PcrdQ'></q></dir></style></legend>
          <tbody id='PcrdQ'></tbody>
      2. <i id='PcrdQ'><tr id='PcrdQ'><dt id='PcrdQ'><q id='PcrdQ'><span id='PcrdQ'><b id='PcrdQ'><form id='PcrdQ'><ins id='PcrdQ'></ins><ul id='PcrdQ'></ul><sub id='PcrdQ'></sub></form><legend id='PcrdQ'></legend><bdo id='PcrdQ'><pre id='PcrdQ'><center id='PcrdQ'></center></pre></bdo></b><th id='PcrdQ'></th></span></q></dt></tr></i><div id='PcrdQ'><tfoot id='PcrdQ'></tfoot><dl id='PcrdQ'><fieldset id='PcrdQ'></fieldset></dl></div>

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

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

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

                  本文介绍了python中'set.intersection()'的算法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  首先,我的目的是在两个已知集合中随机获取一个元素.所以我原来的方法是先相交两组.然后从相交集中随机选取一个元素.但这是愚蠢的,因为我只需要一个元素,但需要一个相交集.

                  First of all, my purpose is to randomly get only one element in both known sets. So my original method is firstly intersect two sets. And then randomly pick up a element from the intersected set. But this is foolish, because that I only need a elements but a intersected set.

                  所以我需要找到set.intersection()的算法.

                  So I need to find the algorithm of set.intersection().

                  我比较了 'set.intersection()' 和 'for{for{}}' 方法的成本时间.Set.intersection() 比其他更快(100 倍).所以使用'for{for{}}'来随机选取一个元素并不是一个明智的主意.

                  I compare the cost time between the methods of 'set.intersection()' and 'for{for{}}'. Set.intersection() is more faster than other one(100 times). So using 'for{for{}}' to pick up a randomly elements is not a wise idea.

                  python 中 set.intersection() 背后的算法是什么?

                  What's the algorithm behind set.intersection() in python?

                  推荐答案

                  算法如下:循环遍历较小的集合,并根据是否在较大的集合中找到每个元素来复制每个元素.所以,它是 C 的等价物

                  The algorithm is as follows: the smaller set is looped over and every element is copied depending whether it's found in the bigger set. So, it's the C equivalent of

                  def intersect(a, b):
                      if len(a) > len(b):
                          a, b = b, a
                  
                      c = set()
                      for x in a:
                          if x in b:
                              c.add(x)
                      return c
                  

                  (或者:return set(x for x in a if x in b).)

                  这篇关于python中'set.intersection()'的算法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何制作一组列表 下一篇:从python中的元组或集合列表中查找不相交的集合

                  相关文章

                    1. <legend id='0wdPk'><style id='0wdPk'><dir id='0wdPk'><q id='0wdPk'></q></dir></style></legend>
                    2. <small id='0wdPk'></small><noframes id='0wdPk'>

                      <tfoot id='0wdPk'></tfoot>
                        <bdo id='0wdPk'></bdo><ul id='0wdPk'></ul>

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