【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基础 索引页