龙空技术网

python实现嵌套列表排序大全「推荐收藏」

yishuihancheng 311

前言:

当前兄弟们对“python嵌套列表长度”大体比较注意,姐妹们都想要剖析一些“python嵌套列表长度”的相关资讯。那么小编同时在网摘上收集了一些有关“python嵌套列表长度””的相关知识,希望咱们能喜欢,小伙伴们快快来了解一下吧!

《python实现嵌套列表按指定位置元素排序、按子列表长度、子列表之和、绝对值和、最大/最小值排序》

本文首发地址:

欢迎关注我的博客【Together_CZ】,我是沂水寒城!

python的编程使用过程中,list列表结构我想是必不可少的,这也是python里面非常非常重要的一种数据结构,list可以用来模拟Queue队列还可以用来模拟Stack堆栈等等,非常的强大,今天在使用列表的时候有一个需求就是需要对嵌套列表进行排序,什么是嵌套列表呢,简单来说就是:列表内的元素也是列表,对于这样的列表对象我一般习惯称之为“嵌套列表”,当然如果只嵌套一层的话你也可以叫它矩阵。

对于列表的排序,可以使用python内置的sort或者是sorted函数来快速地实现升序或者是降序的排序,但是对于嵌套列表这样的方法不能够直接使用了,因为在python中列表本身不是可直接比较的对象,sort和sorted就无法直接使用了,但是,无法直接使用并不代表不能使用,下面是我的具体实践,都很简单,觉得有必要总结一下,就在这里贴一下代码:

#!usr/bin/env python#encoding:utf-8from __future__ import division '''__Author__:沂水寒城功能: 嵌套列表排序''' import operator  def sortFunc1(data_list,index=-3): ''' 借助于 operator 模块实现排序 ''' data_list.sort(key=operator.itemgetter(index))  print '======================= sorted data_list ===================' print data_list  def sortFunc2(data_list,index=-3): ''' 基于内置函数 sorted 实现排序 ''' data_list=sorted(data_list,key=lambda i:i[index]) print '======================= sorted data_list ===================' print data_list data_list.sort(key=lambda i:i[index], reverse=True) print '======================= sorted data_list ===================' print data_list  def sortFunc3(data_list): ''' 依据列表中元素的数量来排序 ''' data_list=sorted(data_list,key=lambda i:len(i)) print '======================= sorted data_list ===================' print data_list  def sortFunc4(data_list): ''' 依据列表中元素总和来排序 ''' data_list=sorted(data_list,key=lambda i:sum(i)) print '======================= sorted data_list ===================' print data_list  def sortFunc5(data_list): ''' 依据列表中元素绝对值的总和来排序 ''' data_list=sorted(data_list,key=lambda i:sum([abs(one) for one in i])) print '======================= sorted data_list ===================' print data_list  def sortFunc6(data_list): ''' 依据列表中元素最大值来排序 ''' data_list=sorted(data_list,key=lambda i:max(i)) print '======================= sorted data_list ===================' print data_list  def sortFunc7(data_list): ''' 依据列表中元素最小值来排序 ''' data_list=sorted(data_list,key=lambda i:min(i)) print '======================= sorted data_list ===================' print data_list  if __name__=='__main__': data_list=[['A',2,3,'apple','p',45,9],['D',23,13,'orange','q',23,19],['C',12,3,'banana','s',22,29],['E',42,23,'water','p',14,98], ['F',2,3,'snow','k',5,8],['G',12,43,'rain','m',41,12],['L',12,33,'ice','n',15,19],['O',12,13,'cream','b',31,25]]  sortFunc1(data_list,index=-3) sortFunc2(data_list,index=-3)   data_list=[['E',42,12,23,'p',14,98],['A','p',45,9],['D',13,'orange','q',23,19],['C',3,'s',22,29],['E',42,12,23,'water','p',14,98]] sortFunc3(data_list)   data_list=[[42,12,23,14,98],[45,9],[13,-100,3,19],[6,3,-5,22,29],[42,12,23,14,98]] sortFunc4(data_list) sortFunc5(data_list) sortFunc6(data_list) sortFunc7(data_list)

测试结果输出如下:

======================= sorted data_list ===================[['O', 12, 13, 'cream', 'b', 31, 25], ['F', 2, 3, 'snow', 'k', 5, 8], ['G', 12, 43, 'rain', 'm', 41, 12], ['L', 12, 33, 'ice', 'n', 15, 19], ['A', 2, 3, 'apple', 'p', 45, 9], ['E', 42, 23, 'water', 'p', 14, 98], ['D', 23, 13, 'orange', 'q', 23, 19], ['C', 12, 3, 'banana', 's', 22, 29]]======================= sorted data_list ===================[['O', 12, 13, 'cream', 'b', 31, 25], ['F', 2, 3, 'snow', 'k', 5, 8], ['G', 12, 43, 'rain', 'm', 41, 12], ['L', 12, 33, 'ice', 'n', 15, 19], ['A', 2, 3, 'apple', 'p', 45, 9], ['E', 42, 23, 'water', 'p', 14, 98], ['D', 23, 13, 'orange', 'q', 23, 19], ['C', 12, 3, 'banana', 's', 22, 29]]======================= sorted data_list ===================[['C', 12, 3, 'banana', 's', 22, 29], ['D', 23, 13, 'orange', 'q', 23, 19], ['A', 2, 3, 'apple', 'p', 45, 9], ['E', 42, 23, 'water', 'p', 14, 98], ['L', 12, 33, 'ice', 'n', 15, 19], ['G', 12, 43, 'rain', 'm', 41, 12], ['F', 2, 3, 'snow', 'k', 5, 8], ['O', 12, 13, 'cream', 'b', 31, 25]]======================= sorted data_list ===================[['A', 'p', 45, 9], ['C', 3, 's', 22, 29], ['D', 13, 'orange', 'q', 23, 19], ['E', 42, 12, 23, 'p', 14, 98], ['E', 42, 12, 23, 'water', 'p', 14, 98]]======================= sorted data_list ===================[[13, -100, 3, 19], [45, 9], [6, 3, -5, 22, 29], [42, 12, 23, 14, 98], [42, 12, 23, 14, 98]]======================= sorted data_list ===================[[45, 9], [6, 3, -5, 22, 29], [13, -100, 3, 19], [42, 12, 23, 14, 98], [42, 12, 23, 14, 98]]======================= sorted data_list ===================[[13, -100, 3, 19], [6, 3, -5, 22, 29], [45, 9], [42, 12, 23, 14, 98], [42, 12, 23, 14, 98]]======================= sorted data_list ===================[[13, -100, 3, 19], [6, 3, -5, 22, 29], [45, 9], [42, 12, 23, 14, 98], [42, 12, 23, 14, 98]]

欢迎交流指导,互相学习提高!

标签: #python嵌套列表长度