change key in OrderedDict without losing order

强颜欢笑 提交于 2019-12-04 15:48:18

问题


Starting with

OrderedDict([('a', 1), ('c', 3), ('b', 2)])

is it possible to end up with

OrderedDict([('a', 1), ('__C__', 3), ('b', 2)])

making sure that the '__C__' item is before 'b' and after 'a' i.e. keeping order?


回答1:


You could try:

>>> d = OrderedDict([('a', 1), ('c', 3), ('b', 2)])
>>> d
OrderedDict([('a', 1), ('c', 3), ('b', 2)])
>>> d2 = OrderedDict([('__C__', v) if k == 'c' else (k, v) for k, v in d.items()])
>>> d2
OrderedDict([('a', 1), ('__C__', 3), ('b', 2)])



回答2:


if you wish to mutate the current dictionary object:

def change_key(self, old, new):
    for _ in range(len(self)):
        k, v = self.popitem(False)
        self[new if old == k else k] = v

This works by iterating over the whole OrderedDict (using its length), and pop'ing its first item (by passing False to .popitem(): the default of this method is to pop the last item) into k and v (respectively standing for key and value); and then inserting this key/value pair, or the new key with its original value, at the end of the OrderedDict.

By repeating this logic for the entire size of the dict, it effectively rotates the dict completely, thus recreating the original order.



来源:https://stackoverflow.com/questions/12150872/change-key-in-ordereddict-without-losing-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!