【Python】二维数组降序排序例子
返回: Python基础 索引页
如下的例子中,将二维数组按照第二维的降序进行排序
### Prepare the list complist = [] tmpFactor= [] tmpFactor.append("[Action-1]") tmpFactor.append(3) complist.append(tmpFactor) tmpFactor= [] tmpFactor.append("[Action-2]") tmpFactor.append(5) complist.append(tmpFactor) tmpFactor= [] tmpFactor.append("[Action-3]") tmpFactor.append(2) complist.append(tmpFactor) tmpFactor= [] tmpFactor.append("[Action-4]") tmpFactor.append(1) complist.append(tmpFactor) tmpFactor= [] tmpFactor.append("[Action-5]") tmpFactor.append(8) complist.append(tmpFactor) tmpFactor= [] tmpFactor.append("[Action-6]") tmpFactor.append(4) complist.append(tmpFactor) tester=0 lcount = len(complist) ### Sort desc for x in range(lcount-1): for y in range(x+1,lcount): print("") if complist[x][1] < complist[y][1]: complist[x],complist[y]=complist[y],complist[x] print("exchanged") else: tester=1 ## Just to make if-else to end easily print(complist) tester=0 ## Just to make the outer for cycle to end easily print (complist)
运行结果如下:
exchanged [['[Action-2]', 5], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-5]', 8], ['[Action-6]', 4]] [['[Action-2]', 5], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-5]', 8], ['[Action-6]', 4]] [['[Action-2]', 5], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-5]', 8], ['[Action-6]', 4]] exchanged [['[Action-5]', 8], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-2]', 5], ['[Action-6]', 4]] [['[Action-5]', 8], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-2]', 5], ['[Action-6]', 4]] [['[Action-5]', 8], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-2]', 5], ['[Action-6]', 4]] [['[Action-5]', 8], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-2]', 5], ['[Action-6]', 4]] exchanged [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-1]', 3], ['[Action-6]', 4]] [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-1]', 3], ['[Action-6]', 4]] [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-1]', 3], ['[Action-6]', 4]] exchanged [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-1]', 3], ['[Action-4]', 1], ['[Action-3]', 2], ['[Action-6]', 4]] exchanged [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-6]', 4], ['[Action-4]', 1], ['[Action-3]', 2], ['[Action-1]', 3]] exchanged [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-6]', 4], ['[Action-3]', 2], ['[Action-4]', 1], ['[Action-1]', 3]] exchanged [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-6]', 4], ['[Action-1]', 3], ['[Action-4]', 1], ['[Action-3]', 2]] exchanged [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-6]', 4], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1]] >>> tester=0 ## Just to make the outer for cycle to end easily >>> print (complist) [['[Action-5]', 8], ['[Action-2]', 5], ['[Action-6]', 4], ['[Action-1]', 3], ['[Action-3]', 2], ['[Action-4]', 1]] >>>
返回: Python基础 索引页
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架