Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中以字母顺序对字母数字字符串进行排序?_Python_Sorting - Fatal编程技术网

如何在python中以字母顺序对字母数字字符串进行排序?

如何在python中以字母顺序对字母数字字符串进行排序?,python,sorting,Python,Sorting,我正在处理一本带有字母数字键的词典,我需要对它们进行排序。 所需的顺序是 a1 : 3 a2 : 2 b1 : 2 b2 : 3 c1 : 5 a1 b2 : 3 a2 b1 : 2 a1 c1 : 3 a2 c1 : 2 b1 c1 : 2 b2 c1 : 3 a1 b2 c1 : 3 a2 b1 c1 : 2 然而,到目前为止,我得到的是 for key in sorted(s1.keys(),key = lambda item: (len(item),item,)): pr

我正在处理一本带有字母数字键的词典,我需要对它们进行排序。 所需的顺序是

a1 : 3
a2 : 2
b1 : 2
b2 : 3
c1 : 5
a1 b2 : 3
a2 b1 : 2
a1 c1 : 3
a2 c1 : 2
b1 c1 : 2
b2 c1 : 3
a1 b2 c1 : 3
a2 b1 c1 : 2
然而,到目前为止,我得到的是

 for key in sorted(s1.keys(),key = lambda item: (len(item),item,)):
      print("%s: %s" % (key, s1[key]))

 a1: 3
 a2: 2
 b1: 2
 b2: 3
 c1: 5
 a1 b2: 3
 a1 c1: 3
 a2 b1: 2
 a2 c1: 2
 b1 c1: 2
 b2 c1: 3
 a1 b2 c1: 3
 a2 b1 c1: 2

问题是我想先按照A->B->C->AB->AC->BC->ABC的顺序,然后根据数值对每个小组进行排序,例如,对于AB,如果我有a1b1,a2b1,a1b2,a2b2,那么顺序将是a1b1,a1b2,a2b1,a2b2

一种可能是扩展您的方法,并在创建排序键时显式划分字母和数字:

d = {
'a1': 3,
'a2': 2,
'b1': 2,
'b2': 3,
'c1': 5,
'a1 b2': 3,
'a2 b1': 2,
'a1 c1': 3,
'a2 c1': 2,
'b1 c1': 2,
'b2 c1': 3,
'a1 b2 c1': 3,
'a2 b1 c1': 2
}

def fn(key):
    letters = key[0::3] #extract the "letter" part of the key
    idx = key[1::3] #extract the "numeric" part of the key

    #construct the composite key
    return (len(letters), letters, idx)

for key in sorted(d.keys(), key = fn):
    print(key, d[key])
产生

('a1', 3)
('a2', 2)
('b1', 2)
('b2', 3)
('c1', 5)
('a1 b2', 3)
('a2 b1', 2)
('a1 c1', 3)
('a2 c1', 2)
('b1 c1', 2)
('b2 c1', 3)
('a1 b2 c1', 3)
('a2 b1 c1', 2)

作为
功能,您可以
拆分
键:

>>> s = 'a1 b2 c1'
>>> list(zip(*s.split()))
[('a', 'b', 'c'), ('1', '2', '1')]
要在
ab
之前对
b
进行排序,还必须考虑段数

对于您的
s1
数据:

>>> sorted(s1, key=lambda s: (s.count(' '), list(zip(*s.split()))))
['a1',
 'a2',
 'b1',
 'b2',
 'c1',
 'a1 b2',
 'a2 b1',
 'a1 c1',
 'a2 c1',
 'b1 c1',
 'b2 c1',
 'a1 b2 c1',
 'a2 b1 c1']
如果每个块可以有多个字母或数字,则可以使用:


看看groupby。但我不确定这将是最优雅的解决方案。字母和数字总是一位数吗?中间总是有一个空间吗?可以有没有数字的字母,也可以有没有数字的字母?@tobias_k是的,格式总是一个字母和一个数字,中间还有一个空格,谢谢你的帮助!谢谢你的帮助!
>>> s = "aa12 bb34 cc56"
>>> re.findall("[a-z]+", s), re.findall("\d+", s)
(['aa', 'bb', 'cc'], ['12', '34', '56'])