首页 > 技术文章 > python 使用sorted方法对二维列表排序

weisunblog 2020-03-05 18:44 原文

list3 = [['1', '15', '8'], ['1', '14', '2'], ['1', '15', '9'], ['1', '14', '3'], ['1', '16', '0'], ['1', '15', '1'],
          ['1', '16', '2'], ['1', '15', '10'], ['1', '16', '3'], ['1', '15', '12']]


def sort3(list1):
  #对数字列表排序 list2 = sorted(list1, key=(lambda x: [x[0],x[1], x[2]]))
  #以内部的一维列表的下标为0的元素为主,下标为1的元素为次,以下标为2的元素最次,进行排序 print(list2) return list2 def to_int(slist):
  #将字符串列表转化为数字列表 num_list = [] for i in slist: r = map(eval, i) num_list.append(list(r)) print(num_list) return num_list

num_list2 = to_int(list3)
sort3(num_list2)

 

推荐阅读