【Python】【排序】字符串列表排序/題目排序/首字母排序


 

一、首字母排序

說明:有一個字符串列表  然后根據字符串的第一個字母 對這個字符串列表進行排序

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Time    :
@Author  :
@File    :
@Version :
@Function:
"""
from typing import List


class SortStr:
    @staticmethod
    def __find_pha(str_olds: List[str]):
        """
        過濾字符串中的非字母字符
        :param str_olds: 原始字符串 如:[--?—He teaches physics i(What is your father)]
        :return: 過濾后的新字符串 如:[HeteachesphysicsiWhatisyourfather]
        """
        str_news = []
        for str_old in str_olds:
            str_new = ''
            for char_old in str_old:
                if char_old.isalpha():
                    str_new += char_old
            str_news.append(str_new)
        return str_news

    @staticmethod
    def __sort_str(str_olds: List[str], reverse: bool = False, target_index: int = 0):
        """
        對字符串列表排序
        :param str_olds: 原始字符串列表
        :param reverse: False-正序,True-倒序
        :param target_index: 需要排序的字符在字符串中的序號(從0開始)
        :return:
        """
        # 全部轉為大寫
        str_olds_tem = []
        for i in str_olds:
            str_olds_tem.append(i.upper())
        # 排序(針對list中的字符串首字母排序)
        sort_index = []
        for i in str_olds_tem:
            sort_index.append({
                'key': str_olds_tem.index(i),
                'value': i[target_index]
            })
        reverse = reverse if reverse else False
        sort_index = sorted(sort_index, key=lambda e: e.__getitem__('value'), reverse=reverse)
        # 排序后的序號
        ture_index = []
        for i in sort_index:
            ture_index.append(i.get('key'))
        return ture_index

    @staticmethod
    def start(str_old, *args, **kwargs):
        """
        開始調用
        :param str_old:
        :param args:
        :param kwargs: reverse=True 倒序 默認正序
        :return:
        """
        str1 = SortStr.__find_pha(str_old)
        ture_index = SortStr.__sort_str(str1, *args, **kwargs)
        ture_str = []
        for i in ture_index:
            ture_str.append(str_old[i])
        return ture_str


if __name__ == '__main__':
    str_o = [
        "--?—de teaches physics i(What is your father)",
        "--?—I’m suffering fr(What's the matter with)",
        "--?—Well, they got there l(How long have y)",
        "--?—bes, what size is t(Anything I can do fo)",
        "--?—Aou too!  (Merry Christmas!)",
        "--?—AAu too!  (Merry Christmas!)",
    ]
    print('*' * 20 + '排序前' + '*' * 20)
    print(*str_o, sep='\n')
    print('\n' + '*' * 20 + '排序后(正序)' + '*' * 20)
    print(*SortStr.start(str_o), sep='\n')
    print('\n' + '*' * 20 + '排序后(倒序)' + '*' * 20)
    print(*SortStr.start(str_o, reverse=True), sep='\n')
    print('\n' + '*' * 20 + '排序后(對第二個字母字符排序)' + '*' * 20)
    print(*SortStr.start(str_o, target_index=1), sep='\n')

輸出

 

 

 

 

 

 

二、所有字母排序

說明:有一個字符串列表  然后根據字符串的 每一個字母 依次遞歸對這個字符串列表進行排序

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM