1. <tfoot id='zMayT'></tfoot>
    1. <legend id='zMayT'><style id='zMayT'><dir id='zMayT'><q id='zMayT'></q></dir></style></legend>
    2. <small id='zMayT'></small><noframes id='zMayT'>

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

    3. 如何在指定位置将键值对插入字典中?

      时间:2023-09-12
      <legend id='iH0Q6'><style id='iH0Q6'><dir id='iH0Q6'><q id='iH0Q6'></q></dir></style></legend>
            <tbody id='iH0Q6'></tbody>
        • <tfoot id='iH0Q6'></tfoot>

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

              1. 本文介绍了如何在指定位置将键值对插入字典中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何在从 YAML 文档加载的 python 字典中的指定位置插入键值对?

                How would I insert a key-value pair at a specified location in a python dictionary that was loaded from a YAML document?

                例如,如果字典是:

                dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

                我希望插入元素 'Phone':'1234'before 'Age'after 'Name' 例如.我要处理的实际字典非常大(解析的 YAML 文件),因此删除和重新插入可能有点麻烦(我真的不知道).

                I wish to insert the element 'Phone':'1234' before 'Age', and after 'Name' for example. The actual dictionary I shall be working on is quite large (parsed YAML file), so deleting and reinserting might be a bit cumbersome (I don't really know).

                如果给我一种插入到 OrderedDict 中指定位置的方法,那也可以.

                If I am given a way of inserting into a specified position in an OrderedDict, that would be okay, too.

                推荐答案

                关于python <3.7(或 cpython <3.6),您无法控制标准字典中对的顺序.

                On python < 3.7 (or cpython < 3.6), you cannot control the ordering of pairs in a standard dictionary.

                如果您打算经常执行任意插入,我的建议是使用列表来存储键,并使用字典来存储值.

                If you plan on performing arbitrary insertions often, my suggestion would be to use a list to store keys, and a dict to store values.

                mykeys = ['Name', 'Age', 'Class']
                mydict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} # order doesn't matter
                
                k, v = 'Phone', '123-456-7890'
                
                mykeys.insert(mykeys.index('Name')+1, k)
                mydict[k] = v
                
                for k in mykeys:
                    print(f'{k} => {mydict[k]}')
                
                # Name => Zara
                # Phone => 123-456-7890
                # Age => 7
                # Class => First
                

                <小时>

                如果您打算使用内容不太可能更改的顺序来初始化字典,则可以使用维护插入顺序的 collections.OrderedDict 结构.

                from collections import OrderedDict
                
                data = [('Name', 'Zara'), ('Phone', '1234'), ('Age', 7), ('Class', 'First')] 
                odict = OrderedDict(data)
                odict
                # OrderedDict([('Name', 'Zara'),
                #              ('Phone', '1234'),
                #              ('Age', 7),
                #              ('Class', 'First')])
                

                请注意,OrderedDict 不支持在任意位置插入(它只记住键插入字典的顺序).

                Note that OrderedDict does not support insertion at arbitrary positions (it only remembers the order in which keys are inserted into the dictionary).

                这篇关于如何在指定位置将键值对插入字典中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 Python 在 YAML 中获取重复键 下一篇:为什么 PyYAML 使用生成器来构造对象?

                相关文章

                最新文章

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

                <tfoot id='2QLtM'></tfoot>

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

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