Python中使用operator模块实现对象的多级排序

今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能。

比如我有如下的类关系,A对象引用了一个B对象,

class A(object):
    def __init__(self, b):
        self.b = b
    def __str__(self):
        return "[%s, %s, %s]" % (self.b.attr1, self.b.attr2, self.b.attr3)
    def __repr__(self):
        return "[%s, %s, %s]" % (self.b.attr1, self.b.attr2, self.b.attr3)

class B(object):
    def __init__(self, attr1, attr2, attr3):
        self.attr1 = attr1
        self.attr2 = attr2
        self.attr3 = attr3
    def __str__(self):
        return "[%s, %s, %s]" % (self.attr1, self.attr2, self.attr3)
    def __repr__(self):
        return "[%s, %s, %s]" % (self.attr1, self.attr2, self.attr3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

下面是测试排序代码,这里是按照A对象的内嵌对象B的attr2和attr3属性来排序。

from operator import itemgetter, attrgetter

a1 = A(B('u1', 'AAA', 100))
a2 = A(B('u2', 'BBB', 100))
a3 = A(B('u3', 'BBB', 10))
aaa = (a1, a2, a3,)

print sorted(aaa, key=attrgetter('b.attr2', 'b.attr3'))
print sorted(aaa, key=attrgetter('b.attr2', 'b.attr3'), reverse=True)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行上面的测试,结果如下:

[[u1, AAA, 100], [u3, BBB, 10], [u2, BBB, 100]]
[[u2, BBB, 100], [u3, BBB, 10], [u1, AAA, 100]]
  • 1
  • 2
 
  • 1
  • 2

那么,如果我需要先按b.attr2正序,再按b.attr3倒序来排序,可以使用下面组合来实现:

s = sorted(aaa, key=attrgetter('b.attr3'), reverse=True)
s = sorted(s, key=attrgetter('b.attr2'))
print s
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

运行结果如下:

[[u1, AAA, 100], [u2, BBB, 100], [u3, BBB, 10]]

http://mobike.365soke.cn/
http://youbai.365soke.cn/
http://qibeitech.365soke.cn/

Python中使用operator模块实现对象的多级排序的更多相关文章

  1. Python中的logging模块

    http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stack ...

  2. 浅析Python中的struct模块

    最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...

  3. python中的StringIO模块

    python中的StringIO模块 标签:python StringIO 此模块主要用于在内存缓冲区中读写数据.模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中.此类中的大部分 ...

  4. python中的select模块

    介绍: Python中的select模块专注于I/O多路复用,提供了select  poll  epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kqu ...

  5. Python中的re模块--正则表达式

    Python中的re模块--正则表达式 使用match从字符串开头匹配 以匹配国内手机号为例,通常手机号为11位,以1开头.大概是这样13509094747,(这个号码是我随便写的,请不要拨打),我们 ...

  6. 【转】浅析Python中的struct模块

    [转]浅析Python中的struct模块 最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概 ...

  7. python中的logger模块

    logger 提供了应用程序可以直接使用的接口handler将(logger创建的)日志记录发送到合适的目的输出filter提供了细度设备来决定输出哪条日志记录formatter决定日志记录的最终输出 ...

  8. Python入门之Python中的logging模块

    基本用法 下面的代码展示了logging最基本的用法. import logging import sys # 获取logger实例,如果参数为空则返回root logger logger = log ...

  9. 深入理解python中的select模块

    简介 Python中的select模块专注于I/O多路复用,提供了select  poll  epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kque ...

随机推荐

  1. Day1 Numerical simulation of optical wave propagation之标量衍射理论基本原理(一)

    <Numerical simulation of optical  wave propagation>内容 1. 介绍光波传输的基础理论.离散采样方法.基于MATLAB平台的编码实例以及具 ...

  2. PS电商产品banner设计

  3. threading模块,python下的多线程

    一.GIL全局解释器锁 In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nativ ...

  4. 用WSDL4J解析types标签中的内容

    WSDL4J是一种用来解析WSDL文本的常用工具. 但网络上用WSDL4J来解析wsdl文档complexType标签中内容的问题一大堆也没有有效的解决方法.今天在我“遍历”wsdl4j的api文档和 ...

  5. 福州大学软件工程1816 | W班 第2次作业成绩排名

    作业链接 词频统计基础功能 评分细则 本次个人项目分数由两部分组成(博客分满分40分+程序得分满分60分) 博客评分规则 在文章开头给出你们Fork仓库的Github项目地址.(1') 在开始实现程序 ...

  6. Java Core - JVM运行时内存管理

    在读正文之前,阅读以下两篇博客学习并理解堆栈.作用域.本地方法的概念. 作用域:https://www.cnblogs.com/AlanLee/p/6627949.html 操作数栈:https:// ...

  7. 为github添加ssh key

    用git关联github上的远程仓库前需要先为github添加ssh key 一.检查本机是否生成ssh key 本地查找.ssh文件,其中id_rsa.pub中的内容就是ssh key 二.为git ...

  8. CodeForces Round #529 Div.3

    http://codeforces.com/contest/1095 A. Repeating Cipher #include <bits/stdc++.h> using namespac ...

  9. 原生JS操作object HTMLTableSectionElement 对象,获取行数

    html页面 <tbody id="infoTab"> <tr class="fomat"> <td class="bl ...

  10. day 7-13 数据库的数据类型

    一. 数据类型 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 注意:int类型的宽度是显示宽度,并非是数据的存储宽度 详细的介绍:http:// ...