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

  • 相关阅读:
    Gym 100818F Irrational Roots (数学)
    学习总结 for循环--冒泡排序
    学习总结 for循环语句的应用
    学习总结 条件语句的应用
    学习总结 运算符了解与应用
    学习记录 彻底搞清 C#中a++与++a的区别
    学习总结 数据类型的应用与转换
    学习总结 数据类型
    学习总结 二进制转换与应用
    学习总结 vs软件简单了解
  • 原文地址:https://www.cnblogs.com/gaojian/p/15975851.html
Copyright © 2020-2023  润新知