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

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

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

      使用列表/元组元素作为键创建字典

      时间:2023-08-31
        <tfoot id='9mBlO'></tfoot>

        <legend id='9mBlO'><style id='9mBlO'><dir id='9mBlO'><q id='9mBlO'></q></dir></style></legend>

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

              <small id='9mBlO'></small><noframes id='9mBlO'>

                <bdo id='9mBlO'></bdo><ul id='9mBlO'></ul>
                本文介绍了使用列表/元组元素作为键创建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要生成这样的字典:

                <代码>{'新环境':{'新项目':{'新比较':{实例":[],'n_thing': '新事物'}}}}

                来自一个元组,像这样:('newEnv','newProj','newComp','newThing') 但前提是它不存在.所以,我尝试了这个:

                myDict = {}(env,proj,comp,thing) = ('newEnv','newProj','newComp','newThing')如果 env 不在 myDict 中:我的字典 [env] = {}如果 proj 不在 myDict[env] 中:myDict[env][proj] = {}如果 comp 不在 myDict[env][proj] 中:myDict[env][proj][comp] = {'n_thing': 东西,'instances': []}

                这非常有效,但不确定它的效率如何,或者我是否应该这样做.有什么建议)??

                解决方案

                可以使用循环(只有前 3 个键,newThing 不是链中的键):

                myDict = {}路径 = ('newEnv','newProj','newComp')当前 = 我的字典对于路径中的键:当前 = current.setdefault(key, {})

                其中 current 最终成为最里面的字典,让您在其上设置 'n_thing''instances' 键.p>

                您可以使用 reduce() 将其折叠成单行:

                myDict = {}路径 = ('newEnv','newProj','newComp')减少(lambda d,k:d.setdefault(k,{}),路径,myDict)

                reduce 调用返回最里面的字典,因此您可以使用它来分配最终值:

                myDict = {}路径 = ('newEnv','newProj','newComp')inner = reduce(lambda d, k: d.setdefault(k, {}), path, myDict)inner.update({'n_thing': 'newThing', 'instances': []})

                I need to generate a dictionary like this:

                {
                  'newEnv': {
                     'newProj': {
                        'newComp': {
                           'instances': [],
                           'n_thing': 'newThing'
                        }
                     }
                  }
                }
                

                from a tuple, like this: ('newEnv','newProj','newComp','newThing') but only if that doesn't already exists. So, I tried this:

                myDict = {}
                (env,proj,comp,thing) = ('newEnv','newProj','newComp','newThing')
                
                if env not in myDict:
                    myDict[env] = {}
                if proj not in myDict[env]:
                    myDict[env][proj] = {}
                if comp not in myDict[env][proj]:
                    myDict[env][proj][comp] = {'n_thing': thing, 'instances': []}
                

                which is pretty much working but not sure how efficient is that or if I should be doing this way at all. Any suggestion(s)??

                解决方案

                You can use a loop (with just the first 3 keys, newThing is not a key in the chain):

                myDict = {}
                path = ('newEnv','newProj','newComp')
                current = myDict
                for key in path:
                    current = current.setdefault(key, {})
                

                where current ends up as the innermost dictionary, letting you set the 'n_thing' and 'instances' keys on that.

                You could use reduce() to collapse that into a one-liner:

                myDict = {}
                path = ('newEnv','newProj','newComp')
                reduce(lambda d, k: d.setdefault(k, {}), path, myDict)
                

                The reduce call returns the innermost dictionary, so you can use that to assign your final value:

                myDict = {}
                path = ('newEnv','newProj','newComp')
                inner = reduce(lambda d, k: d.setdefault(k, {}), path, myDict)
                inner.update({'n_thing': 'newThing', 'instances': []})
                

                这篇关于使用列表/元组元素作为键创建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为相同的字典值创建可交换元组键的最佳方法是什么? 下一篇:在元组或对象列表上使用 Python 的 list index() 方法?

                相关文章

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

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

                  <tfoot id='xS4DQ'></tfoot>
                  • <bdo id='xS4DQ'></bdo><ul id='xS4DQ'></ul>

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