Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- d = collections.OrderedDict()
- d['a'] = 'A'
- d['b'] = 'B'
- d['c'] = 'C'
- d['d'] = 'D'
- d['e'] = 'E'
- >>> import collections
- >>> d = collections.OrderedDict()
- >>> d['a'] = 'A'
- >>> d['b'] = 'B'
- >>> d['c'] = 'C'
- >>> d['d'] = 'D'
- >>> d['e'] = 'E'
- >>> new_order = ['e','a','b','c','d']
- >>> type(d)((k, d[k]) for k in new_order)
- OrderedDict([('e', 'E'), ('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')])
- >>> d = OrderedDict()
- >>> for c in 'ABCDE':
- ... d[c.lower()] = c
- ...
- >>> d
- OrderedDict([('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D'), ('e', 'E')])
- >>> d.move_to_end('e',last=False) # last=False moves to beginning
- >>> d
- OrderedDict([('e', 'E'), ('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')])
- new_keys = ['e','a','b','c','d']
- new_dict = collections.OrderedDict()
- for i in new_keys:
- new_dict[i] = d[i]
Add Comment
Please, Sign In to add comment