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

Original: https://www.cnblogs.com/gaojian/p/15975851.html
Author: 健哥的数据花园
Title: 【Python】二维数组降序排序例子

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/9348/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部