-2

有没有一种简单的方法来做一个包含字母和数字字符的“文件柜”类型的字符串?

即一个列表,例如:

[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)
4

1 回答 1

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']
于 2013-05-07T11:18:17.727 回答