Override the {…} notation so i get an OrderedDict() instead of a dict()?

后端 未结 7 672
野的像风
野的像风 2020-11-28 21:38

Update: dicts retaining insertion order is guaranteed for Python 3.7+

I want to use a .py file like a config file. So using the {.

7条回答
  •  难免孤独
    2020-11-28 21:45

    OrderedDict is not "standard python syntax", however, an ordered set of key-value pairs (in standard python syntax) is simply:

    [('key1 name', 'value1'), ('key2 name', 'value2'), ('key3 name', 'value3')]
    

    To explicitly get an OrderedDict:

    OrderedDict([('key1 name', 'value1'), ('key2 name', 'value2'), ('key3 name', 'value3')])
    

    Another alternative, is to sort dictname.items(), if that's all you need:

    sorted(dictname.items())
    

提交回复
热议问题