Sorting dict items by key, beyond alphanumeric sorting

为君一笑 提交于 2019-12-02 06:48:31

问题


I have written this code:

n=5
dizN={}
for q in range(0,n+1):
    h=n-q
    dizN['a'+str(q)+'p'+str(h)]=0

that creates such a dictionary:

dizN

Out[120]: {'a0p5': 0, 'a1p4': 0, 'a2p3': 0, 'a3p2': 0, 'a4p1': 0, 'a5p0': 0}

Note that "n" is the basic parameter for my code. As you can see, the sum of integers present in dict keys string is always =n (=5 in this case, where n=5).

It is important for me (for more difficult purposes in my program) that, for every n anyone can choose, the dict is ordered in this way:

{'a0p(n)': 0, 'a1p(n-1)': 0, ....., 'a(n-1)p1': 0, 'a(n)p0': 0}

My code is ok, but only for n<10. If n is >=10, this is what happens: (n=12) dizN:

Out[121]: 
  {'a0p12': 0,
   'a10p2': 0,
   'a11p1': 0,
   'a12p0': 0,
   'a1p11': 0,
   'a2p10': 0,
   'a3p9': 0,
   'a4p8': 0,
   'a5p7': 0,
   'a6p6': 0,
   'a7p5': 0,
   'a8p4': 0,
   'a9p3': 0}

As you can see the interpreter follows alphanumeric sorting;

Anybody know if there is a way to obtain the same dict sorted this way:

{'a0p12': 0,
 'a1p11': 0,
 'a2p10': 0,
 'a3p9': 0,
 'a4p8': 0,
 'a5p7': 0,
 'a6p6': 0,
 'a7p5': 0,
 'a8p4': 0,
 'a9p3': 0,
 'a10p2': 0,
 'a11p1': 0,
 'a12p0': 0}

?

I know that dictionaries are basically non sortable, but i hope somebody knows some trick to obtain my purpose anyway :)

Thanks a lot!


回答1:


dicts are unordered, so to get the order you are going to have to sort the items and use an OrderedDict to maintain the sorted order. To get the order you want you can create tuples from the groups of integers so you sort as integers in lexicographical order:

from itertools import groupby
from collections import OrderedDict
d = {'a0p12': 0, 'a10p2': 0, 'a11p1': 0, 'a12p0': 0, 'a1p11': 0, 'a2p10': 0,
     'a3p9': 0, 'a4p8': 0, 'a5p7': 0, 'a6p6': 0, 'a7p5': 0, 'a8p4': 0, 'a9p3': 0}

def key_func(x):
    """'a0p12' -> (0, 12)"""
    return tuple(int("".join(v)) for k,v in groupby(x[0], key=str.isdigit) if k)
od = OrderedDict(sorted(d.items(), key=key_func))

print(od)

Which would give you:

OrderedDict([('a0p12', 0), ('a1p11', 0), ('a2p10', 0), ('a3p9', 0), 
('a4p8', 0), ('a5p7', 0), ('a6p6', 0), ('a7p5', 0), ('a8p4', 0), 
('a9p3', 0), ('a10p2', 0), ('a11p1', 0), ('a12p0', 0)])

You could also use a regex to find the groups of digits:

from collections import OrderedDict
import re

d = {'a0p12': 0, 'a10p2': 0, 'a11p1': 0, 'a12p0': 0, 'a1p11': 0, 'a2p10': 0,
     'a3p9': 0, 'a4p8': 0, 'a5p7': 0, 'a6p6': 0, 'a7p5': 0, 'a8p4': 0, 'a9p3': 0}



def key_func(x,patt=re.compile("\d+")):
    """'a0p12' -> (0, 12)"""
    return tuple(map(int, patt.findall(x[0])))

od = OrderedDict(sorted(d.items(), key=key_func))

print(od)


来源:https://stackoverflow.com/questions/40088527/sorting-dict-items-by-key-beyond-alphanumeric-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!