• <bdo id='2FVwB'></bdo><ul id='2FVwB'></ul>

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

      <legend id='2FVwB'><style id='2FVwB'><dir id='2FVwB'><q id='2FVwB'></q></dir></style></legend>

        <tfoot id='2FVwB'></tfoot>
        <i id='2FVwB'><tr id='2FVwB'><dt id='2FVwB'><q id='2FVwB'><span id='2FVwB'><b id='2FVwB'><form id='2FVwB'><ins id='2FVwB'></ins><ul id='2FVwB'></ul><sub id='2FVwB'></sub></form><legend id='2FVwB'></legend><bdo id='2FVwB'><pre id='2FVwB'><center id='2FVwB'></center></pre></bdo></b><th id='2FVwB'></th></span></q></dt></tr></i><div id='2FVwB'><tfoot id='2FVwB'></tfoot><dl id='2FVwB'><fieldset id='2FVwB'></fieldset></dl></div>
      1. 在 Python 中复制字典的快速方法

        Fast way to copy dictionary in Python(在 Python 中复制字典的快速方法)
        <i id='0vlr7'><tr id='0vlr7'><dt id='0vlr7'><q id='0vlr7'><span id='0vlr7'><b id='0vlr7'><form id='0vlr7'><ins id='0vlr7'></ins><ul id='0vlr7'></ul><sub id='0vlr7'></sub></form><legend id='0vlr7'></legend><bdo id='0vlr7'><pre id='0vlr7'><center id='0vlr7'></center></pre></bdo></b><th id='0vlr7'></th></span></q></dt></tr></i><div id='0vlr7'><tfoot id='0vlr7'></tfoot><dl id='0vlr7'><fieldset id='0vlr7'></fieldset></dl></div>
        • <small id='0vlr7'></small><noframes id='0vlr7'>

            • <bdo id='0vlr7'></bdo><ul id='0vlr7'></ul>

            • <tfoot id='0vlr7'></tfoot>

              • <legend id='0vlr7'><style id='0vlr7'><dir id='0vlr7'><q id='0vlr7'></q></dir></style></legend>

                    <tbody id='0vlr7'></tbody>
                  本文介绍了在 Python 中复制字典的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Python 程序,可以经常使用字典.我必须复制字典数千次.我需要密钥和相关内容的副本.副本将被编辑,并且不得链接到原件(例如,副本中的更改不得影响原件.)

                  I have a Python program that works with dictionaries a lot. I have to make copies of dictionaries thousands of times. I need a copy of both the keys and the associated contents. The copy will be edited and must not be linked to the original (e.g. changes in the copy must not affect the original.)

                  键是字符串,值是整数 (0/1).

                  Keys are Strings, Values are Integers (0/1).

                  我目前使用一个简单的方法:

                  I currently use a simple way:

                  newDict = oldDict.copy()
                  

                  分析我的代码显示复制操作花费了大部分时间.

                  Profiling my Code shows that the copy operation takes most of the time.

                  dict.copy() 方法是否有更快的替代方法?什么是最快的?

                  Are there faster alternatives to the dict.copy() method? What would be fastest?

                  推荐答案

                  看C源码 对于 Python dict 操作,您可以看到它们做了一个非常幼稚(但高效)的复制.它本质上归结为对 PyDict_Merge 的调用:

                  Looking at the C source for the Python dict operations, you can see that they do a pretty naive (but efficient) copy. It essentially boils down to a call to PyDict_Merge:

                  PyDict_Merge(PyObject *a, PyObject *b, int override)
                  

                  这会快速检查它们是否是同一个对象以及它们是否有对象.之后,它会对目标 dict 进行一次慷慨的 resize/alloc,然后一个一个地复制元素.我没有看到你比内置的 copy() 快得多.

                  This does the quick checks for things like if they're the same object and if they've got objects in them. After that it does a generous one-time resize/alloc to the target dict and then copies the elements one by one. I don't see you getting much faster than the built-in copy().

                  这篇关于在 Python 中复制字典的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <tfoot id='9te4o'></tfoot>

                    <bdo id='9te4o'></bdo><ul id='9te4o'></ul>

                      • <small id='9te4o'></small><noframes id='9te4o'>

                            <tbody id='9te4o'></tbody>
                          <legend id='9te4o'><style id='9te4o'><dir id='9te4o'><q id='9te4o'></q></dir></style></legend>

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