• <legend id='FKcAw'><style id='FKcAw'><dir id='FKcAw'><q id='FKcAw'></q></dir></style></legend>

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

        <bdo id='FKcAw'></bdo><ul id='FKcAw'></ul>
      <tfoot id='FKcAw'></tfoot>

      1. 字典是在 Python 3.6+ 中排序的吗?

        Are dictionaries ordered in Python 3.6+?(字典是在 Python 3.6+ 中排序的吗?)

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

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

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

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

              1. <tfoot id='iPBwx'></tfoot>
                • 本文介绍了字典是在 Python 3.6+ 中排序的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Dictionaries are ordered in Python 3.6 (under the CPython implementation at least) unlike in previous incarnations. This seems like a substantial change, but it's only a short paragraph in the documentation. It is described as a CPython implementation detail rather than a language feature, but also implies this may become standard in the future.

                  How does the new dictionary implementation perform better than the older one while preserving element order?

                  Here is the text from the documentation:

                  dict() now uses a "compact" representation pioneered by PyPy. The memory usage of the new dict() is between 20% and 25% smaller compared to Python 3.5. PEP 468 (Preserving the order of **kwargs in a function.) is implemented by this. The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5). (Contributed by INADA Naoki in issue 27350. Idea originally suggested by Raymond Hettinger.)

                  Update December 2017: dicts retaining insertion order is guaranteed for Python 3.7

                  解决方案

                  Are dictionaries ordered in Python 3.6+?

                  They are insertion ordered[1]. As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python (and other ordered behavior[1]).

                  As of Python 3.7, this is no longer an implementation detail and instead becomes a language feature. From a python-dev message by GvR:

                  Make it so. "Dict keeps insertion order" is the ruling. Thanks!

                  This simply means that you can depend on it. Other implementations of Python must also offer an insertion ordered dictionary if they wish to be a conforming implementation of Python 3.7.


                  How does the Python 3.6 dictionary implementation perform better[2] than the older one while preserving element order?

                  Essentially, by keeping two arrays.

                  • The first array, dk_entries, holds the entries (of type PyDictKeyEntry) for the dictionary in the order that they were inserted. Preserving order is achieved by this being an append only array where new items are always inserted at the end (insertion order).

                  • The second, dk_indices, holds the indices for the dk_entries array (that is, values that indicate the position of the corresponding entry in dk_entries). This array acts as the hash table. When a key is hashed it leads to one of the indices stored in dk_indices and the corresponding entry is fetched by indexing dk_entries. Since only indices are kept, the type of this array depends on the overall size of the dictionary (ranging from type int8_t(1 byte) to int32_t/int64_t (4/8 bytes) on 32/64 bit builds)

                  In the previous implementation, a sparse array of type PyDictKeyEntry and size dk_size had to be allocated; unfortunately, it also resulted in a lot of empty space since that array was not allowed to be more than 2/3 * dk_size full for performance reasons. (and the empty space still had PyDictKeyEntry size!).

                  This is not the case now since only the required entries are stored (those that have been inserted) and a sparse array of type intX_t (X depending on dict size) 2/3 * dk_sizes full is kept. The empty space changed from type PyDictKeyEntry to intX_t.

                  So, obviously, creating a sparse array of type PyDictKeyEntry is much more memory demanding than a sparse array for storing ints.

                  You can see the full conversation on Python-Dev regarding this feature if interested, it is a good read.


                  In the original proposal made by Raymond Hettinger, a visualization of the data structures used can be seen which captures the gist of the idea.

                  For example, the dictionary:

                  d = {'timmy': 'red', 'barry': 'green', 'guido': 'blue'}
                  

                  is currently stored as [keyhash, key, value]:

                  entries = [['--', '--', '--'],
                             [-8522787127447073495, 'barry', 'green'],
                             ['--', '--', '--'],
                             ['--', '--', '--'],
                             ['--', '--', '--'],
                             [-9092791511155847987, 'timmy', 'red'],
                             ['--', '--', '--'],
                             [-6480567542315338377, 'guido', 'blue']]
                  

                  Instead, the data should be organized as follows:

                  indices =  [None, 1, None, None, None, 0, None, 2]
                  entries =  [[-9092791511155847987, 'timmy', 'red'],
                              [-8522787127447073495, 'barry', 'green'],
                              [-6480567542315338377, 'guido', 'blue']]
                  

                  As you can visually now see, in the original proposal, a lot of space is essentially empty to reduce collisions and make look-ups faster. With the new approach, you reduce the memory required by moving the sparseness where it's really required, in the indices.


                  [1]: I say "insertion ordered" and not "ordered" since, with the existence of OrderedDict, "ordered" suggests further behavior that the `dict` object *doesn't provide*. OrderedDicts are reversible, provide order sensitive methods and, mainly, provide an order-sensive equality tests (`==`, `!=`). `dict`s currently don't offer any of those behaviors/methods.
                  [2]: The new dictionary implementations performs better **memory wise** by being designed more compactly; that's the main benefit here. Speed wise, the difference isn't so drastic, there's places where the new dict might introduce slight regressions (key-lookups, for example) while in others (iteration and resizing come to mind) a performance boost should be present. Overall, the performance of the dictionary, especially in real-life situations, improves due to the compactness introduced.

                  这篇关于字典是在 Python 3.6+ 中排序的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  • <bdo id='IH5Zz'></bdo><ul id='IH5Zz'></ul>
                    <tfoot id='IH5Zz'></tfoot>
                  • <i id='IH5Zz'><tr id='IH5Zz'><dt id='IH5Zz'><q id='IH5Zz'><span id='IH5Zz'><b id='IH5Zz'><form id='IH5Zz'><ins id='IH5Zz'></ins><ul id='IH5Zz'></ul><sub id='IH5Zz'></sub></form><legend id='IH5Zz'></legend><bdo id='IH5Zz'><pre id='IH5Zz'><center id='IH5Zz'></center></pre></bdo></b><th id='IH5Zz'></th></span></q></dt></tr></i><div id='IH5Zz'><tfoot id='IH5Zz'></tfoot><dl id='IH5Zz'><fieldset id='IH5Zz'></fieldset></dl></div>
                    <legend id='IH5Zz'><style id='IH5Zz'><dir id='IH5Zz'><q id='IH5Zz'></q></dir></style></legend>
                      <tbody id='IH5Zz'></tbody>

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