按字母顺序排列dict项目,超出字母数字排序 [英] Sorting dict items by key, beyond alphanumeric sorting

查看:163
本文介绍了按字母顺序排列dict项目,超出字母数字排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码:

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

创建这样的字典:

dizN

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

请注意,n是我的代码的基本参数。正如你所看到的,在dict键字符串中存在的整数的总和总是= n(在这种情况下为5,其中n = 5)。

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).

重要的是我(为了在我的程序中更困难的目的),对于任何人,任何人都可以选择,dict以这种方式订购:

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}

我的代码可以,但只适用于n <10。
如果n是> = 10,这是会发生什么:(n = 12)dizN:

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 :)

非常感谢!

推荐答案

,所以要获得订单,您将不得不对项目进行排序,并使用 OrderedDict 维护排序顺序。要获得您想要的顺序,您可以从整数组中创建元组,以便按字典顺序排列为整数:

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)

哪个会给你:

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)

这篇关于按字母顺序排列dict项目,超出字母数字排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆