简体   繁体   English

Python:multidict排序

[英]Python: multidict ordering

i'm trying do order a python dict and i'm trying to find the most pythonic way to do it. 我正在尝试订购python字典,而我正在尝试找到最pythonic的方式来做到这一点。 Here is what the dict looks like: 字典看起来像这样:

{
 u'key1': {'a': 'name', 'number': 282},
 u'key2': {'a': 'name2','number': 1421},
 u'key3': {'a': 'name3', 'number': 95}, 
}

How can i order this dict to this: 我该如何命令这个字典:

{
 u'key2': {'a': 'name2','number': 1421},
 u'key1': {'a': 'name', 'number': 282},
 u'key3': {'a': 'name3', 'number': 95}, 
}

Ordering by the number key in the intern dict? 通过实习字典中的数字键排序? Thanks for helping! 感谢您的帮助!

If you want a dict that maintains a sorted order, you need a different data structure. 如果要保留排序顺序的字典,则需要其他数据结构。 If you search PyPI you'll find a variety of options. 如果您搜索PyPI ,则会找到多种选择。

Some of them are built on top of data structures designed for this kind of thing (red-black trees, B-trees, skip lists), so they'll do everything pretty quickly. 其中一些建立在针对此类事物设计的数据结构之上(红黑树,B树,跳过列表),因此它们将很快完成所有操作。 Others work by just re-sorting every time you modify them (or do a smarter variation—re-sorting every time you access them after one or more modification). 其他人则通过在每次修改它们时进行重新排序来进行工作(或进行更智能的更改-在一次或多次修改后每次访问它们时都进行重新排序)。

I don't want to recommend one in particular and stop you from reading over a few of them, so I'll just show how to do it with the first one that comes back in the search, sorteddict , which I've never used: 我不想特别推荐一个,也不要阻止您阅读其中的一些内容,因此,我将向您展示如何使用搜索中返回的第一个搜索结果sorteddict ,我从未使用过:

sd = sorteddict.sorteddict({
        u'key1': {'a': 'name', 'number': 282},
        u'key2': {'a': 'name2','number': 1421},
        u'key3': {'a': 'name3', 'number': 95}, 
    }, key=operator.itemgetter('number'))

However, if you're just building the dict all at once and never modifying it, it's easier (and probably faster) to use an OrderedDict , as explained in inspectorG4dget's answer. 但是,如果您只是一次构建所有字典而从不修改它,那么使用OrderedDict会更容易(可能更快),如inspectorG4dget的答案所述。 Just put the keys and values in sorted order first. 只需将键和值放在先后即可。 Or even use sorted to do it for you: 甚至可以使用sorted为您做到:

od = collections.OrderedDict(sorted(d.items(), key=lambda item: item[1]['number']))

If you want to do this manually, you can do it by using an "index list": a list (or OrderedDict or sortedlist or sorteddict ) of keys that you build or maintain in order, and use to find the key to look up. 如果要手动执行此操作,则可以使用“索引列表”来进行操作:按顺序构建或维护的键的列表(或OrderedDictsortedlistsorteddict ),并用于查找要查找的键。 But there's really no good reason to do this unless you need to have multiple ways of indexing the same mapping (or to learn how OrderedDict works under the covers—but for that, start by reading the Ordered Dictionary for Py2.4 recipe linked from the docs). 但是,除非您需要多种方法为同一映射建立索引(或者了解OrderedDict工作方式,否则实际上并没有充分的理由这样做),为此,请先阅读从Py2.4链接的Ped2.4有序词典。 docs)。

Dictionaries are unordered. 字典是无序的。 The values they hold are accessed by the keys with which those values are associated. 它们所保存的值由与这些值相关联的键访问。
Thus, if you wanted the dictionary {'a': 'name', 'number': 282} , you would access it by calling myDict[u'key1'] . 因此,如果您想要字典{'a': 'name', 'number': 282} ,则可以通过调用myDict[u'key1']来访问它。

It is very rarely the case that ordering the keys in a dict is a required/useful feature. dict对键进行排序是必需/有用的功能,这种情况很少见。

However, for whatever reason, if you still want to order the dict somehow, you could look into using a collections.OrderedDict , which will preserve the order in which you insert key,value pairs 但是,无论出于何种原因,如果您仍然想以某种方式对字典进行排序,都可以考虑使用collections.OrderedDict ,它将保留插入键值对的顺序。

If you want a list of those inner dict s ordered by the value associated with their 'number' keys, then this will do it: 如果dict与它们的'number'键关联的值排序的那些内部dict的列表,则可以这样做:

myList = sorted(myDict.values(), key=lambda d:d['number'])

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM