如何对字母数字字符串进行“文件柜”排序

时间:2013-05-07 11:16:27

标签: python string sorting alpha numeric

是否有一种简单的方法可以使用包含字母和数字字符的“文件柜”字符串?

即。列表如:

  

[aa2, ab2, bb1, bc1, ab3, aa3, ba2, ba1, aa1, ac1, bb2, bb3, ab1]

排序为

  

[aa1, aa2, aa3, ab1, ab2, ab3, ac1, ba1, ba2, bb1, bb2, bb3, bc1]

我有一个想法,一个脚本将它们分解成单个字符,对每一组进行排序,然后重新组装它们,但这似乎很难。 : - )


这里是最终的代码,效果很好。感谢来自Experts Exchange D0t k0m的pepr和jamylak在问题被杀之前回答。内置的排序无法处理排序的数字部分。即他们把数字排序为词汇......

  

[aa1, aa11, aa111, aa2, aa3, ba1, ba11, ba2]

......而不是......

  

[aa1, aa2, aa3, aa11, aa111, ba1, ba2, ba11]

因此,这种Jamylak代码的变化很有效......

def lexinumericalsort(_list):
    def _key(item):
        # pattern will split strings and letters up to 8 times (intended for device file names)
        pattern = ('([a-zA-Z]{0,})(\d{0,})') * 8 
        try:
            result = re.match(pattern, item).groups()
            result = list(result)
            for no, val in enumerate(result):
                if val is '':
                    result.pop(no) # Remove blanks
                try:
                    # Convert to integer if possible
                    result[no] = int(result[no]) 
                except ValueError, e:
                    pass #silence if fails (leaves as character)
            return result
        except AttributeError, e:
            e = ("functions.lexinumericalsort:\n" + 
                 "Unable to resolve list into pattern matched groups.\n" +
                 str(e))
            print e
            return None

    return sorted(_list, key = _key)

1 个答案:

答案 0 :(得分:1)

>>> sorted("aa2, ab2, bb1, bc1, ab3, aa3, ba2, ba1, aa1, ac1, bb2, bb3, ab1".split(', '))
['aa1', 'aa2', 'aa3', 'ab1', 'ab2', 'ab3', 'ac1', 'ba1', 'ba2', 'bb1', 'bb2', 'bb3', 'bc1']

虽然你的意思是:

>>> items = ['a11', 'a2']
>>> sorted(items)
['a11', 'a2']

哪里排序不起作用。只需使用正则表达式模式匹配您的数据,并将key函数传递给sorted

>>> import re
>>> def key(item):
        g = re.match(r'([a-zA-Z]+)(\d+)', item).groups()
        return g[0], int(g[1])

>>> sorted(items, key=key)
['a2', 'a11']