How to sort dictionary by key in numerical order Python

后端 未结 4 739
时光取名叫无心
时光取名叫无心 2020-12-01 15:59

Here is the dictionary looks like:

{\'57481\': 50, \'57480\': 89, \'57483\': 110, \'57482\': 18, \'57485\': 82, \'57484\': 40}  

I would

相关标签:
4条回答
  • 2020-12-01 16:34

    Standard Python dicts are "unordered". You can use an OrderedDict, take a look at the docs:

    from collections import OrderedDict
    
    d = {'57481': 50, '57480': 89, '57483': 110, '57482': 18, '57485': 82, '57484': 40}
    OrderedDict(sorted(d.items(), key=lambda t: t[0]))
    # OrderedDict([('57480', 89), ('57481', 50), ('57482', 18), ('57483', 110), ('57484', 40), ('57485', 82)])
    
    0 讨论(0)
  • 2020-12-01 16:36

    In Python 3 sorted() has an optional parameter key. And in 3.6+ dict maintains insertion order.

    key specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower). The default value is None (compare the elements directly).

    Therefore, what OP wants can be accomplished this way.

    >>> d = {'57481': 50, '57480': 89, '57483': 110, '57482': 18, '57485': 82, '57484': 40}
    >>> for key, value in sorted(d.items(), key=lambda item: int(item[0])):
    ...     print(key, value)
    57480 89
    57481 50
    57482 18
    57483 110
    57484 40
    57485 82
    

    Or if OP wants to create a new sorted dictionary.

    >>> d = {'57481': 50, '57480': 89, '57483': 110, '57482': 18, '57485': 82, '57484': 40}
    >>> d_sorted = {key:value for key, value in sorted(d.items(), key=lambda item: int(item[0]))}
    >>> d_sorted
    {'57480': 89, '57481': 50, '57482': 18, '57483': 110, '57484': 40, '57485': 82}
    

    d.items() returns a list of tuples, e.g. ('57480': 89) and so on. The lambda functions takes this tuple and applies int function to the first value. And then the result is used for comparison.

    0 讨论(0)
  • 2020-12-01 16:50

    If you only need to sort by key, you're 95% there already. Assuming your dictionary seems to be called docs_info:

    for key, value in sorted(docs_info.items()): # Note the () after items!
        print(key, value)
    

    Since dictionary keys are always unique, calling sorted on docs_info.items() (which is a sequence of tuples) is equivalent to sorting only by the keys.

    Do bear in mind that strings containing numbers sort unintuitively! e.g. "11" is "smaller" than "2". If you need them sorted numerically, I recommend making the keys int instead of str; e.g.

    int_docs_info = {int(k) : v for k, v in docss_info.items()}
    

    This of course just changes the order in which you access the dictionary elements, which is usually sufficient (since if you're not accessing it, what does it matter if it's sorted?). If for some reason you need the dict itself to be "sorted", then you'll have to use collections.OrderedDict, which remembers the order in which items were inserted into it. So you could first sort your dictionary (as above) and then create an OrderedDict from the sorted (key, value) pairs:

    sorted_docs_info = collections.OrderedDict(sorted(docs_info.items()))
    
    0 讨论(0)
  • 2020-12-01 16:53

    If repeatedly sorting elements and inserting them in an ordered dict is too slow, consider one of the sorted dict implementations on PyPI. A SortedDict data type efficiently maintains its keys in sorted order. The sortedcontainers module contains one such implementation.

    Installation from PyPI is easy:

    pip install sortedcontainers
    

    If you can't pip install then simply copy down the sortedlist.py and sorteddict.py files from the open-source repository. SortedContainers is implemented in pure-Python but is fast-as-C implementations.

    Once installed simply:

    In [1]: from sortedcontainers import SortedDict
    
    In [6]: SortedDict({'57481': 50, '57480': 89, '57483': 110, '57482': 18, '57485': 82, '57484': 40})
    Out[6]: SortedDict({'57480': 89, '57481': 50, '57482': 18, '57483': 110, '57484': 40, '57485': 82})
    

    The sortedcontainers module also maintains a performance comparison of several popular implementations.

    0 讨论(0)
提交回复
热议问题