<legend id='VVAnu'><style id='VVAnu'><dir id='VVAnu'><q id='VVAnu'></q></dir></style></legend>
    • <bdo id='VVAnu'></bdo><ul id='VVAnu'></ul>

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

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

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

      1. 通过将列表转换为集合并重新转换为列表来对列表进行排序的时间复杂性

        Time complexity in sorting a list by converting it to a set and back into a list(通过将列表转换为集合并重新转换为列表来对列表进行排序的时间复杂性)

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

                <tfoot id='D6MwG'></tfoot>
                  <bdo id='D6MwG'></bdo><ul id='D6MwG'></ul>
                  <legend id='D6MwG'><style id='D6MwG'><dir id='D6MwG'><q id='D6MwG'></q></dir></style></legend>
                    <tbody id='D6MwG'></tbody>
                  本文介绍了通过将列表转换为集合并重新转换为列表来对列表进行排序的时间复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我最近看了Raymond Hettingers talk about python dictionaries(以及扩展集.)他还提到,整数会自行散列,将整数加到字典(或集合……)将按顺序插入它们,只要您不删除项,顺序将在python3.6中保留(可能在更高版本?)。在this question的回答中,说明字典保持插入顺序,但是对于集合,它看起来就像整数是根据它们的值排序的。

                  现在:根据time-complexity section of python.org和更详细的here,将一个元素添加到集合的平均时间复杂度为O(1)。这意味着如果您有一个未排序的整数列表,只需执行以下操作即可对它们进行排序:

                  sorted_list = list(set(unsorted_list))
                  

                  就我测试过的情况而言,似乎就是这种情况(用随机序列测试了1000次)。

                  我现在的问题是:这是否意味着可以在O(N)时间内对python中的整数进行排序?

                  对我来说,这是接缝的,因为构建集合需要O(N),将集合转换回列表需要O(N),还是我在这里遗漏了什么?

                  推荐答案

                  否。设置不允许对整数进行排序。而hashes of integers are well-defined集合的顺序为任意

                  集的顺序可能因实现、流程和实例而异。

                  # CPython 3.7.4
                  >>> list({1, 8})
                  [8, 1]
                  >>> list({8, 1})
                  [8, 1]
                  # PyPy 3.6.9 (PyPy 7.3.0)
                  >>> list({1, 8})
                  [1, 8]
                  >>> list({8, 1})
                  [8, 1]
                  # CPython 2.7.10
                  >>> list({1, 8})
                  [8, 1]
                  >>> list({8, 1})
                  [8, 1]
                  # Jython 2.7.1 (java13.0.2)
                  >>> list({1, 8})
                  [1, 8]
                  >>> list({8, 1})
                  [1, 8]
                  

                  集的顺序还可能取决于实例的历史记录。

                  # CPython 3.7.4
                  >>> a = {1, 3, 4, 8}
                  >>> list(a)
                  [8, 1, 3, 4]
                  >>> a.add(2)
                  >>> list(a)
                  [1, 2, 3, 4, 8]
                  >>> a.discard(2)
                  >>> list(a)
                  [1, 3, 4, 8]
                  

                  这篇关于通过将列表转换为集合并重新转换为列表来对列表进行排序的时间复杂性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

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

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

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