檔案名稱排序 字串序+數字序 python

來源:互聯網
上載者:User

標籤:val   pass   tar   min   exce   new   指令碼   start   class   

# -*-coding:utf8-*-"""基於字串數字混合排序的Python指令碼"""def is_number(s):    try:        float(s)        return True    except ValueError:        pass    try:        import unicodedata        unicodedata.numeric(s)        return True    except (TypeError, ValueError):        pass    return Falsedef find_continuous_num(astr, c):    """    :param astr: a string    :param c: where to start looking for    :return: num: int    """    num = ‘‘    try:        while not is_number(astr[c]) and c < len(astr):            c += 1        while is_number(astr[c]) and c < len(astr):            num += astr[c]            c += 1    except:        pass    if num != ‘‘:        return int(num)def comp2filename(file1, file2):    """    compare 2 filename:    if the prev part of 2 strings are the same, compare the next continuous number    file1 < file2 : return True, otherwise False    :param file1:    :param file2:    :return:    """    smaller_length = min(len(file1), len(file2))    continuous_num = ‘‘    for c in range(0, smaller_length):        if not is_number(file1[c]) and not is_number(file2[c]):            # print(‘both not number‘)            if file1[c] < file2[c]:                return True            if file1[c] > file2[c]:                return False            if file1[c] == file2[c]:                if c == smaller_length - 1:                    # print(‘the last bit‘)                    if len(file1) < len(file2):                        return True                    else:                        return False                else:                    continue        if is_number(file1[c]) and not is_number(file2[c]):            return True        if not is_number(file1[c]) and is_number(file2[c]):            return False        if is_number(file1[c]) and is_number(file2[c]):            if find_continuous_num(file1, c) < find_continuous_num(file2, c):                return True            else:                return False    # if file1 < file2:    #     return True    # else:    #     return Falsedef sort_insert(lst):    """    simple insert sort    :param lst:    :return:    """    for i in range(1, len(lst)):        x = lst[i]        j = i        while j > 0 and lst[j-1] > x:        # while j > 0 and comp2filename(x, lst[j-1]):            lst[j] = lst[j-1]            j -= 1        lst[j] = x    return lstdef sort_insert_filename(lst):    """    simple insert sort    :param lst:    :return:    """    for i in range(1, len(lst)):        x = lst[i]        j = i        # while j > 0 and lst[j-1] > x:        while j > 0 and comp2filename(x, lst[j-1]):            lst[j] = lst[j-1]            j -= 1        lst[j] = x    return lstdef file_name_sort(all_file_list):    """    :param all_file_list: list    :return: new_list:list    """    new_list = []    # all_file_list.sort(key=lambda x: int(x.split(‘.‘)[0].split(‘_‘)[2]))    # for file in all_file_list:    #     pass    return new_listif __name__ == "__main__":    print(sort_insert_filename([‘a09‘, ‘a2‘, ‘b2‘, ‘a10‘,‘a100‘, ‘a01‘, ‘a010‘, ‘_a3‘, ‘a893‘, ‘a90‘]))

檔案名稱排序 字串序+數字序 python

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.