python: sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | # encoding: utf-8 # 版权所有 2024 ©涂聚文有限公司 # 许可信息查看: # 描述: # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # Datetime : 2024/09/28 20:28 # User : geovindu # Product : PyCharm # Project : EssentialAlgorithms # File : example.py from enum import Enum class PepoleCollation(Enum): """ 選擇字段排序 """ Id = 1 , """ """ RealName = 2 , """ """ Tel = 3 , """ """ Age = 4 """ """ class Pepole( object ): """ """ def __init__( self ): """ """ self ._id = None self ._realname = None self ._tel = None self ._age = None @property def Id ( self ): """ """ return self ._id @Id .setter def Id ( self ,ids): """ """ self ._id = ids @property def RealName( self ): """ :return: """ return self ._realname @RealName .setter def RealName( self ,realname): """ :param realname: :return: """ self ._realname = realname @property def Tel( self ): """ :return: """ return self ._tel @Tel .setter def Tel( self ,tel): """ :param tel: :return: """ self ._tel = tel @property def Age( self ): """ :return: """ return self ._age @Age .setter def Age( self ,age): """ :param age: :return: """ self ._age = age def Sort(table: list [Pepole],check: PepoleCollation,isDesc = True ) - > list [Pepole]: """ object Pepole list :param table :param check :param isDesc :return """ #check=PepoleCollationField.Age if PepoleCollation.Age = = check: print ( "age desc" ,PepoleCollation.Age) table.sort(key = lambda Pepole: (Pepole.Age,Pepole. Id ), reverse = isDesc) elif PepoleCollation. Id = = check: table.sort(key = lambda Pepole: (Pepole. Id ), reverse = isDesc) elif PepoleCollation.RealName = = check: table.sort(key = lambda Pepole: (Pepole.RealName), reverse = isDesc) elif PepoleCollation.Tel = = check: table.sort(key = lambda Pepole: (Pepole.Tel), reverse = isDesc) else : table.sort(key = lambda Pepole: (Pepole.RealName), reverse = isDesc) #for info in table: #print(info.Id,info.RealName,info.Tel,info.Age) return table table = [] p = Pepole() p. Id = '1' p.RealName = "geovindu" p.Tel = "13824350518" p.Age = 28 table.append(p) p = Pepole() p. Id = '2' p.RealName = "sibo" p.Tel = "13324350518" p.Age = 18 table.append(p) p = Pepole() p. Id = '3' p.RealName = "zaoti" p.Tel = "13624350518" p.Age = 22 table.append(p) p = Pepole() p. Id = '4' p.RealName = "jiang" p.Tel = "13524350518" p.Age = 12 table.append(p) p = Pepole() p. Id = '5' p.RealName = "xiaobiao" p.Tel = "13224350518" p.Age = 20 table.append(p) # 1 tablesore = sorted (table, key = lambda Pepole: Pepole.Age, reverse = True ) for info in tablesore: print (info. Id ,info.RealName,info.Tel,info.Age) print ( '***************' ) # 2 Sort(table, PepoleCollation.Age, False ) print ( '*******2********' ) for info in table: print (info. Id ,info.RealName,info.Tel,info.Age) # 3 Sort(table, PepoleCollation.Age) print ( '*******3********' ) for info in table: print (info. Id ,info.RealName,info.Tel,info.Age) # from https://docs.python.org/3/howto/descriptor.html # from https://docs.python.org/3/library/functions.html # from https://docs.python.org/3/howto/sorting.html |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | # encoding: utf-8 # 版权所有 2024 ©涂聚文有限公司 # 许可信息查看: # 描述: # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # Datetime : 2024/09/28 20:28 # User : geovindu # Product : PyCharm # Project : EssentialAlgorithms # File : example.py class Pepole( object ): """ """ def __init__( self ): """ """ self ._id = None self ._realname = None self ._tel = None self ._age = None @property def Id ( self ): """ """ return self ._id @Id .setter def Id ( self ,ids): """ """ self ._id = ids @property def RealName( self ): """ :return: """ return self ._realname @RealName .setter def RealName( self ,realname): """ :param realname: :return: """ self ._realname = realname @property def Tel( self ): """ :return: """ return self ._tel @Tel .setter def Tel( self ,tel): """ :param tel: :return: """ self ._tel = tel @property def Age( self ): """ :return: """ return self ._age @Age .setter def Age( self ,age): """ :param age: :return: """ self ._age = age table = [] p = Pepole() p. Id = '1' p.RealName = "geovindu" p.Tel = "13824350518" p.Age = 28 table.append(p) p = Pepole() p. Id = '2' p.RealName = "sibo" p.Tel = "13324350518" p.Age = 18 table.append(p) p = Pepole() p. Id = '3' p.RealName = "zaoti" p.Tel = "13624350518" p.Age = 22 table.append(p) p = Pepole() p. Id = '4' p.RealName = "jiang" p.Tel = "13524350518" p.Age = 12 table.append(p) p = Pepole() p. Id = '5' p.RealName = "xiaobiao" p.Tel = "13224350518" p.Age = 20 table.append(p) # 1 tablesore = sorted (table, key = lambda Pepole: Pepole.Age, reverse = True ) for info in tablesore: print (info. Id ,info.RealName,info.Tel,info.Age) # 2 table.sort(key = lambda Pepole: (Pepole.Age,Pepole. Id ), reverse = True ) for info in table: print (info. Id ,info.RealName,info.Tel,info.Age) # from https://docs.python.org/3/howto/descriptor.html # from https://docs.python.org/3/library/functions.html # from https://docs.python.org/3/howto/sorting.html |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | # encoding: utf-8 # 版权所有 2024 ©涂聚文有限公司 # 许可信息查看: # 描述: # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # Datetime : 2024/4/28 10:37 # User : geovindu # Product : PyCharm # Project : EssentialAlgorithms # File : example.py # explain : 学习 class Sort( object ): """ """ @staticmethod def BubbleSort(table: list , columnindex = 4 ) - > list : """ :param table: Two Dimensional (2D) Array :param columnindex: 需要排序的列索引 :return: tabe sorted """ if type (table[ 0 ]) = = list : #二维列表 for i in range ( 0 , len (table) - 1 ): # 考虑数据类型 # print(table[i][j],table[i+1][j],table[i],table[i+1]) if type (table[i][columnindex]) = = int : if table[i][columnindex] < table[i + 1 ][columnindex]: temp = table[i] table[i] = table[i + 1 ] table[i + 1 ] = temp if type (table[i][columnindex]) = = str : if table[i][columnindex][ 0 ].lower() < table[i + 1 ][columnindex][ 0 ].lower(): # 第一个字母排序,以小写字母 以小写字母,以字母的ASCII的值比較 temp = table[i] table[i] = table[i + 1 ] table[i + 1 ] = temp else : # 一维列表 for i in range ( len (table)): for j in range ( 0 , len (table) - i - 1 ): if type (table[i]) = = int : if table[j] > table[j + 1 ]: temp = table[j] table[j] = table[j + 1 ] table[j + 1 ] = temp if type (table[i]) = = str : if table[j][ 0 ] > table[j + 1 ][ 0 ]: temp = table[j] table[j] = table[j + 1 ] table[j + 1 ] = temp return table if __name__ = = "__main__" : table = [ [ '1' , 'Du' , 'GeovinDu' , '13824350518' , 92 ], [ '2' , 'Rose' , 'Tom' , '1882458888' , 38 ], [ '3' , 'Lin' , 'bo' , '852000000' , 87 ], [ '4' , 'Ada' , 'Jaing' , '18999999999' , 87 ]] print ( type (table)) print ( type (table[ 0 ])) if type (table[ 0 ]) = = list : print ( 'list' ) else : print ( 'not list' ) Sort.BubbleSort(table, 4 ) print (table) row = [ 1 , 5 , 7 , 0 , 92 ] print ( type (row)) print ( type (row[ 0 ])) if type (row[ 0 ])! = list : print ( 'not list' ) else : print ( 'is list' ) Sort.BubbleSort(row) print (row) row = [ '1' , 'Du' , 'GeovinDu' , '13824350518' , '92' ] Sort.BubbleSort(row) print (row) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | table = [ [ '1' , 'Du' , 'GeovinDu' , '13824350518' , 92 ], [ '2' , 'Rose' , 'Tom' , '1882458888' , 38 ], [ '3' , 'Lin' , 'bo' , '852000000' , 87 ], [ '4' , 'Ada' , 'Jaing' , '18999999999' , 87 ]] # Bubble Sort冒泡排序法 columnindex = 1 print ( type (table[ 0 ][columnindex]),table[ 0 ][coumnindex]) for i in range ( 0 , len (table) - 1 ): #for j in range(0, len(table[0]) - i - 1): #print(table[i][j],table[i+1][j],table[i],table[i+1]) if type (table[i][columnindex]) = = int : if table[i][columnindex]<table[i + 1 ][columnindex]: temp = table[i] table[i] = table[i + 1 ] table[i + 1 ] = temp if type (table[i][columnindex]) = = str : if table[i][columnindex][ 0 ].lower()<table[i + 1 ][columnindex][ 0 ].lower(): temp = table[i] table[i] = table[i + 1 ] table[i + 1 ] = temp print (table) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | table = [ [ '1' , 'Du' , 'GeovinDu' , '13824350518' , 92 ], [ '2' , 'Rose' , 'Tom' , '1882458888' , 38 ], [ '3' , 'Lin' , 'bo' , '852000000' , 87 ], [ '4' , 'Ada' , 'Jaing' , '18999999999' , 87 ]] # Bubble Sort冒泡排序法 curr = 0 tablesore = [] columnindex = 4 for i in range ( 0 , len (table) - 1 ): #for j in range(0, len(table[0]) - i - 1): #print(table[i][j],table[i+1][j],table[i],table[i+1]) if table[i][columnindex] >table[i + 1 ][columnindex]: temp = table[i] table[i] = table[i + 1 ] table[i + 1 ] = temp print (table) print ( '*********' ) table = [ [ '1' , 'Du' , 'GeovinDu' , '13824350518' , 92 ], [ '2' , 'Rose' , 'Tom' , '1882458888' , 38 ], [ '3' , 'Lin' , 'bo' , '852000000' , 87 ], [ '4' , 'Ada' , 'Jaing' , '18999999999' , 87 ]] table.sort(key = lambda column: (column[ 4 ],column[ 0 ] ), reverse = True ) print (table) table.sort(key = lambda column: (column[ 4 ]), reverse = True ) print (table) table.sort(key = lambda column: (column[ 4 ],column[ 0 ]), reverse = True ) print (table) sorted (table, key = lambda x: x[ 4 ], reverse = True ) print (table) # from https://scripteverything.com/python-2d-list-sort-by-multiple-columns-code-examples-no-imports-one-liners # from https://numpy.org/doc/stable/reference/generated/numpy.sort.html # from https://thispointer.com/sorting-2d-numpy-array-by-column-or-row-in-python/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def BubbleSort(table: list , columnindex: int ) - > list : """ :param table: Two Dimensional (2D) Array :param columnindex: 需要排序的列索引 :return: tabe sorted """ for i in range ( 0 , len (table) - 1 ): # 考虑数据类型 # print(table[i][j],table[i+1][j],table[i],table[i+1]) if type (table[i][columnindex]) = = int : if table[i][columnindex] < table[i + 1 ][columnindex]: temp = table[i] table[i] = table[i + 1 ] table[i + 1 ] = temp if type (table[i][columnindex]) = = str : if table[i][columnindex][ 0 ].lower() < table[i + 1 ][columnindex][ 0 ].lower(): # 第一个字母排序,以小写字母 以小写字母,以字母的ASCII的值比較 temp = table[i] table[i] = table[i + 1 ] table[i + 1 ] = temp return table |
1 2 3 4 5 6 7 8 9 10 | if __name__ = = "__main__" : """ main import """ table = [[ '1' , 'Du' , 'GeovinDu' , '13824350518' , 92 ], [ '2' , 'Rose' , 'Tom' , '1882458888' , 38 ], [ '3' , 'Lin' , 'bo' , '852000000' , 87 ], [ '4' , 'Ada' , 'Jaing' , '18999999999' , 87 ]] BubbleSort(table, 4 ) print (table) |
https://docs.python.org/3/library/functions.html
https://docs.python.org/3/howto/descriptor.html
https://docs.python.org/3/howto/descriptor.html
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2023-10-06 typescript: Adapter pattern
2023-10-06 typescript: Singleton Pattern
2022-10-06 CSharp: Composite Pattern in donet core 3
2022-10-06 CSharp: Flyweight Pattern in donet core 3
2022-10-06 Python: Factory Method Pattern