operator.itemgetter函数

operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),要注意,operator.itemgetter函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。

eg:

 students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
 sorted(students, key=lambda student : student[2])
 
key指定的lambda函数功能是去元素student的第三个域(即:student[2]),因此sorted排序时,会以students所有元素的第三个域来进行排序。有了上面的operator.itemgetter函数,也可以用该函数来实现,例如要通过student的第三个域排序,可以这么写:
         sorted(students, key=operator.itemgetter(2)) 
sorted函数也可以进行多级排序,例如要根据第二个域和第三个域进行排序,可以这么写:
         sorted(students, key=operator.itemgetter(1,2)) 
 #-*- encoding=utf-8 -*-  

 import operator
#按字典值排序(默认为升序)
x = {1:2, 3:4, 4:3, 2:1, 0:0}
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1))
print sorted_x
#[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
#如果要降序排序,可以指定reverse=True
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1), reverse=True)
print sorted_x
#[(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]
#或者直接使用list的reverse方法将sorted_x顺序反转
#sorted_x.reverse() #取代方法是,用lambda表达式
sorted_x = sorted(x.iteritems(), key=lambda x : x[1])
print sorted_x
#[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
sorted_x = sorted(x.iteritems(), key=lambda x : x[1], reverse=True)
print sorted_x
#[(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)] #包含字典dict的列表list的排序方法与dict的排序类似,如下:
x = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]
sorted_x = sorted(x, key=operator.itemgetter('name'))
print sorted_x
#[{'age': 10, 'name': 'Bart'}, {'age': 39, 'name': 'Homer'}]
sorted_x = sorted(x, key=operator.itemgetter('name'), reverse=True)
print sorted_x
#[{'age': 39, 'name': 'Homer'}, {'age': 10, 'name': 'Bart'}]
sorted_x = sorted(x, key=lambda x : x['name'])
print sorted_x
#[{'age': 10, 'name': 'Bart'}, {'age': 39, 'name': 'Homer'}]
sorted_x = sorted(x, key=lambda x : x['name'], reverse=True)
print sorted_x
#[{'age': 39, 'name': 'Homer'}, {'age': 10, 'name': 'Bart'}]

  

Python字典按值排序、包含字典的列表按字典值排序的方法的更多相关文章

  1. Python第三天 序列 数据类型 数值 字符串 列表 元组 字典

    Python第三天 序列  数据类型  数值  字符串  列表  元组  字典 数据类型数值字符串列表元组字典 序列序列:字符串.列表.元组序列的两个主要特点是索引操作符和切片操作符- 索引操作符让我 ...

  2. Python 使用 lambda() 结合sort() 或 sorted() 对列表嵌套字典格式的数据进行排序

    1.使用sort()方法进行排序 my_list = [{"age":65, "money":5}, {"age":35, "mo ...

  3. python整理之(字符串、元组、列表、字典)

    一.关于字符串的整理总结 对于字符串的操作常用的有这些: 字符串的操作通过dir()函数可以查看 我们先整理没有下划线的用法,有下划线的暂时不去考虑. 1.capitalize 功能:使字符串的首字母 ...

  4. Python基础(冒泡、生成器、迭代器、列表与字典解析)

    一.冒泡算法 冒泡算法,给定一组数据,从大到小排序或者从小到大排序,就像气泡一样 原理:  相邻的两个对象相比,大的放到后面,交换位置 交换位置通过a,b=b,a来实现 1.我们可以通过for循环来根 ...

  5. (四)Python中的“四大才子”(字符串、列表、字典、集合)

    前戏:在python中把数据序列分为可变(mutable)和不可变(immutable)两种 不可变:string.int.float.tuple 特点:相同对象只是占用一个内存地址,不管有多少个变量 ...

  6. python开发学习-day02(元组、字符串、列表、字典深入)

    s12-20160109-day02 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  7. python 全栈开发,Day7(元组转换,列表以及字典的坑,集合,关系测试,深浅copy,编码补充)

    一.元组转换 数字 tu = (1) tu1 = (1,) print(tu,type(tu)) print(tu1,type(tu1)) 执行输出: 1 <class 'int'>(1, ...

  8. Python中几种数据结构的整理,列表、字典、元组、集合

    列表:shoplist = ['apple', 'mango', 'carrot', 'banana']字典:di = {'a':123,'b':'something'}集合:jihe = {'app ...

  9. Python基础之(判断,循环,列表,字典)

    一.python介绍 Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程.Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在 ...

  10. Python语法基础之对象(字符串、列表、字典、元组)

    转载自https://blog.csdn.net/lijinlon/article/details/81630154 字符串 String 定义:S = 'spam' 索引:S[0] = 's';S[ ...

随机推荐

  1. FreeTextBox备忘

    添加对4.0的dll文件引用 吧aspnet_client目录 copy到根目录下 设置文件上传目录属性ImageGalleryPath 设置相册属性到 ftb.imagegallery.aspx位置 ...

  2. java常用设计模式一:单例模式

     1.饿汉式 package singleton.demo; /** * @author Administrator * @date 2019/01/07 */ public class Single ...

  3. timescale

    `timescale 1ns/100ps     表示时延单位为1ns, 时延精度为100ps.`timescale 编译器指令在模块说明外部出现, 并且影响后面所有的时延值.

  4. nodejs中使用mongodb quickstart

    nodejs中使用mongodb quickstart node 中使用mongodb的quick start.整理的官网crud简单例子. 在百度找了几篇帖子都有问题,所以直接看官网了. 连接Mon ...

  5. hbase使用MapReduce操作4(实现将 HDFS 中的数据写入到 HBase 表中)

    实现将 HDFS 中的数据写入到 HBase 表中 Runner类 package com.yjsj.hbase_mr2; import com.yjsj.hbase_mr2.ReadFruitFro ...

  6. 动态载入DLL所需要的三个函数详解(LoadLibrary,GetProcAddress,FreeLibrary)

    动态载入 DLL 动态载入方式是指在编译之前并不知道将会调用哪些 DLL 函数, 完全是在运行过程中根据需要决定应调用哪些函数. 方法是:用 LoadLibrary 函数加载动态链接库到内存,用 Ge ...

  7. 读《深入理解Windows Phone 8.1 UI控件编程》1.4.3 框架的应用示例:自定义弹出窗口有感

    前些天买了园子里林政老师的两本 WP8.1 的书籍.毕竟想要学得深入的话,还是得弄本书跟着前辈走的. 今天读到 1.4.3 节——框架的应用示例:自定义弹出窗口这一小节.总的来说,就是弄一个像 Mes ...

  8. System.Windows.Freezable 在未被引用的程序集中定义

    System.Windows.Freezable 在未被引用的程序集中定义 解决方法 添加windowsbase.dll 引用

  9. [LeetCode 题解] Spiral Matrix

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...

  10. SQL Server Extended Events 进阶 2:使用UI创建基本的事件会话

    第一阶中我们描述了如何在Profiler中自定义一个Trace,并且让它运行在服务器端来创建一个Trace文件.然后我们通过Jonathan Kehayias的 sp_SQLskills_Conver ...