[英]How to change index values of a dict?
Is it possible to change the values of a sorted dictionary?是否可以更改已排序字典的值?
After sorting the dict alphabetically I got a result like this:按字母顺序对 dict 进行排序后,我得到了这样的结果:
OrderedDict([('a', {'index': 3, 'parent': None}),
('a/b', {'index': 2, 'parent': 'a'}),
('a/b/c', {'index': 4, 'parent': 'a/b'}),
('a/b/d', {'index': 1, 'parent': 'a/b'})])
This dict has a bunch more of key/value pairs like that, my expected output is to maintain it sorted alphabetically but change the indexes value so it looks like this:这个字典有更多这样的键/值对,我期望的 output 是保持它按字母顺序排序但改变索引值,所以它看起来像这样:
OrderedDict([('a', {'index': 1, 'parent': None}),
('a/b', {'index': 2, 'parent': 'a'}),
('a/b/c', {'index': 3, 'parent': 'a/b'}),
('a/b/d', {'index': 4, 'parent': 'a/b'})])
How can I do that?我怎样才能做到这一点?
Iterate over the dictionary's values and set each value's index
:遍历字典的值并设置每个值的
index
:
for i, value in enumerate(dct.values(), start=1):
value['index'] = i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.