zoukankan      html  css  js  c++  java
  • 二维数组的排序

     1 result =[['7250','1'],['7510','2'],['7759','0'],['6523','33']]
     2 
     3 copy_result = []
     4 
     5 for item in result:
     6     copy_result.append([int(item[0]),item[1]])
     7 
     8 print result
     9 # output:
    10 # [['7250', '1'], ['7510', '2'], ['7759', '0'], ['6523', '33']]
    11 
    12 print copy_result
    13 # output
    14 # [[7250, '1'], [7510, '2'], [7759, '0'], [6523, '33']]
    15 
    16 copy_result.sort()
    17 
    18 print copy_result
    19 # output
    20 # [[6523, '33'], [7250, '1'], [7510, '2'], [7759, '0']]
    21 
    22 back_result = []
    23 
    24 for item in copy_result:
    25     back_result.append([str(item[0]),item[1]])
    26 
    27 print back_result
    28 # output
    29 # [['6523', '33'], ['7250', '1'], ['7510', '2'], ['7759', '0']]
     
    用int()和str()转换数据类型

    二维数组的排序 sort()方法根据第一维的排序 
     

    如果是逆序,用reverse()

  • 相关阅读:
    决策树
    交叉熵与softmax
    集成学习
    SVM算法
    蒙特卡罗方法
    K近邻--KNN
    K-Means聚类
    DBSCAN密度聚类
    Bagging、随机森林
    支持向量机SVM
  • 原文地址:https://www.cnblogs.com/tmmuyb/p/3897591.html
Copyright © 2011-2022 走看看