如何更改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'})])
这个字典有更多这样的键/值对,我的预期输出是保持它按字母顺序排序,但更改索引值,使其看起来像这样:
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'})])
我该怎么做那?
Is it possible to change the values of a sorted dictionary?
After sorting the dict alphabetically I got a result like this:
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

迭代字典的值并设置每个值的
索引
:Iterate over the dictionary's values and set each value's
index
: