python 数字排序

#encoding=utf-8  

    print '中国'  

    #根据字符串中的数字排序,如f10应该在f2后面  

    import re  

    re_digits = re.compile(r'(\d+)')  

    def emb_numbers(s):  
        pieces=re_digits.split(s)  
        pieces[1::2]=map(int,pieces[1::2])      
        return pieces  

    def sort_strings_with_emb_numbers(alist):  
        aux = [(emb_numbers(s),s) for s in alist]  
        aux.sort()  
        return [s for __,s in aux]  

    def sort_strings_with_emb_numbers2(alist):  
        return sorted(alist, key=emb_numbers)  

    filelist='file10.txt file2.txt file1.txt'.split()  

    print filelist  

    print '--DSU排序'  
    print sort_strings_with_emb_numbers(filelist)  

    print '--内置DSU排序'  
    print sort_strings_with_emb_numbers2(filelist)

打印结果如下:

中国
[‘file10.txt’, ‘file2.txt’, ‘file1.txt’]
–DSU排序
[‘file1.txt’, ‘file2.txt’, ‘file10.txt’]
–内置DSU排序
[‘file1.txt’, ‘file2.txt’, ‘file10.txt’]

你可能感兴趣的:(python算法)