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

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

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

    1. dict() 和 {} 有什么区别?

      What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
          <tbody id='mb0va'></tbody>
        <tfoot id='mb0va'></tfoot>

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

              1. 本文介绍了dict() 和 {} 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我想制作一本字典.我们称之为d.但是有多种方法可以在 Python 中初始化字典!例如,我可以这样做:

                So let's say I wanna make a dictionary. We'll call it d. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:

                d = {'hash': 'bang', 'slash': 'dot'}
                

                或者我可以这样做:

                d = dict(hash='bang', slash='dot')
                

                或者这个,奇怪的是:

                d = dict({'hash': 'bang', 'slash': 'dot'})
                

                或者这个:

                d = dict([['hash', 'bang'], ['slash', 'dot']])
                

                dict() 函数还有其他多种方式.所以很明显 dict() 提供的东西之一是语法和初始化的灵活性.但这不是我要问的.

                And a whole other multitude of ways with the dict() function. So obviously one of the things dict() provides is flexibility in syntax and initialization. But that's not what I'm asking about.

                假设我要让 d 只是一个空字典.当我执行 d = {}d = dict() 时,Python 解释器的幕后发生了什么?只是做同一件事的两种方法吗?使用 {}additional 调用 dict() 吗?一个有(甚至可以忽略不计)比另一个更多的开销?虽然这个问题真的完全不重要,但我很想回答这个问题.

                Say I were to make d just an empty dictionary. What goes on behind the scenes of the Python interpreter when I do d = {} versus d = dict()? Is it simply two ways to do the same thing? Does using {} have the additional call of dict()? Does one have (even negligible) more overhead than the other? While the question is really completely unimportant, it's a curiosity I would love to have answered.

                推荐答案

                >>> def f():
                ...     return {'a' : 1, 'b' : 2}
                ... 
                >>> def g():
                ...     return dict(a=1, b=2)
                ... 
                >>> g()
                {'a': 1, 'b': 2}
                >>> f()
                {'a': 1, 'b': 2}
                >>> import dis
                >>> dis.dis(f)
                  2           0 BUILD_MAP                0
                              3 DUP_TOP             
                              4 LOAD_CONST               1 ('a')
                              7 LOAD_CONST               2 (1)
                             10 ROT_THREE           
                             11 STORE_SUBSCR        
                             12 DUP_TOP             
                             13 LOAD_CONST               3 ('b')
                             16 LOAD_CONST               4 (2)
                             19 ROT_THREE           
                             20 STORE_SUBSCR        
                             21 RETURN_VALUE        
                >>> dis.dis(g)
                  2           0 LOAD_GLOBAL              0 (dict)
                              3 LOAD_CONST               1 ('a')
                              6 LOAD_CONST               2 (1)
                              9 LOAD_CONST               3 ('b')
                             12 LOAD_CONST               4 (2)
                             15 CALL_FUNCTION          512
                             18 RETURN_VALUE        
                

                dict() 显然是一些 C 内置的.一个非常聪明或敬业的人(不是我)可以查看解释器源并告诉您更多信息.我只是想炫耀一下dis.dis.:)

                dict() is apparently some C built-in. A really smart or dedicated person (not me) could look at the interpreter source and tell you more. I just wanted to show off dis.dis. :)

                这篇关于dict() 和 {} 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                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 与否?)
                Python - Initializing Multiple Lists/Line(Python - 初始化多个列表/行)
                • <small id='OI6nu'></small><noframes id='OI6nu'>

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

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

                          <tbody id='OI6nu'></tbody>

                        • <legend id='OI6nu'><style id='OI6nu'><dir id='OI6nu'><q id='OI6nu'></q></dir></style></legend>
                          <tfoot id='OI6nu'></tfoot>