比如自定义了一个class,并且实例化了这个类的很多个实例,并且组成一个数组。这个数组要排序,是通过这个class的某个字段来排序的。怎么排序呢?

有两种做法:

  • 第一种是定义__cmp__( )方法;

  • 第二种是在sorted( )函数中为key指定一个lambda函数,lambda函数用来排序。

举例(这里都是降序排序,所以指定了reserved=True,可以忽略):

第一种方法:

class BBox(object):
def __init__(self, name, score, x1, y1, x2, y2):
self.name = name
self.score = score
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def __str__(self):
ret_str = 'name:{:s}, score:{:f}, x1={:d},y1={:d},x2={:d},y2={:d}'.format(
self.name, self.score, self.x1, self.y1, self.x2, self.y2
)
return ret_str
def __cmp__(self, other):
return cmp(self.score, other.score) det test():
x1 = 1
y1 = 3
x2 = 6
y2 = 9
b1 = BBox('box1', 0.5, x1, y1, x2, y2)
b2 = BBox('box2', 0.7, x1, y1, x2, y2)
b3 = BBox('box3', 0.3, x1, y1, x2, y2)
box_lst = [b1, b2, b3]
box_lst = sorted(box_lst, reverse=True)
for box in box_lst:
print(box)

第二种方法:

class BBox(object):
def __init__(self, name, score, x1, y1, x2, y2):
self.name = name
self.score = score
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def __str__(self):
ret_str = 'name:{:s}, score:{:f}, x1={:d},y1={:d},x2={:d},y2={:d}'.format(
self.name, self.score, self.x1, self.y1, self.x2, self.y2
)
return ret_str det test():
x1 = 1
y1 = 3
x2 = 6
y2 = 9
b1 = BBox('box1', 0.5, x1, y1, x2, y2)
b2 = BBox('box2', 0.7, x1, y1, x2, y2)
b3 = BBox('box3', 0.3, x1, y1, x2, y2)
box_lst = [b1, b2, b3]
box_lst = sorted(box_lst, key=lambda box: box.score, reserved=True)
for box in box_lst:
print(box)

Python自定义排序的更多相关文章

  1. python 自定义排序函数

    自定义排序函数 Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 ...

  2. python自定义排序函数

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  3. Python自定义排序及我实际遇到的一些题目实例

    写在前面,本文主要介绍Python基础排序和自定义排序的一些规则,如果都比较熟悉,可以直接翻到第三节,看下实际的笔试面试题中关于自定义排序的应用. 一.基础排序 排序是比较基础的算法,与很多语言一样, ...

  4. python中自定义排序函数

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  5. python之自定义排序函数sorted()

    sorted()也是一个高阶函数,它可以接收一个比较函数来实现自定义排序,比较函数的定义是,传入两个待比较的元素 x, y,如果 x 应该排在 y 的前面,返回 -1,如果 x 应该排在 y 的后面, ...

  6. Python:Base4(map,reduce,filter,自定义排序函数(sorted),返回函数,闭包,匿名函数(lambda) )

    1.python把函数作为参数: 在2.1小节中,我们讲了高阶函数的概念,并编写了一个简单的高阶函数: def add(x, y, f): return f(x) + f(y) 如果传入abs作为参数 ...

  7. Python应用——自定义排序全套方案

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天的这篇文章和大家聊聊Python当中的排序,和很多高级语言一样,Python封装了成熟的排序函数.我们只需要调用内部的sort函数,就可 ...

  8. Python进阶之自定义排序函数sorted()

    sorted() .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",& ...

  9. python 顺序读取文件夹下面的文件(自定义排序方式)

    我们在读取文件夹下面的文件时,有时是希望能够按照相应的顺序来读取,但是 file_lists=os.listdir()返回的文件名不一定是顺序的,也就是说结果是不固定的.就比如读取下面这些文件,希望能 ...

随机推荐

  1. Thymeleaf模板布局

    ⒈定义片段 1.使用th:fragment <div th:fragment="copy"> © 2019 <a href="http://www.co ...

  2. 集合排序 Comparator和Comparable的使用区别

    Java 排序 Compare  Comparator接口 Comparable接口 区别 在Java中使用集合来存储数据时非常常见的,集合排序功能也是常用功能之一.下面看一下如何进行集合排序,常用的 ...

  3. ARMV7-M数据手册---Part A :Application Level Architecture---A1 Introduction

    1.前言 本章主要介绍了ARMV7体系结构及其定义的属性,以及本手册定义的ARMV7M属性. 主要包括: ARMV7体系结构和属性 ARMV7M属性 ARMV7M扩展 2. ARMV7体系结构和属性 ...

  4. Class的isAssignableFrom方法

    Class类的isAssignableFrom是个不常用的方法,感觉这个方法的名字取得不是很好,所以有必要在此解析一下,以免在看源码时产生歧义,这个方法的签名如下: public native boo ...

  5. Nginx 开启目录下载

    平时应用中,我们大都用apache搭建下载页面.毕竟Apache搭建起来非常方便,yum安装,创建目录就可以了. 但有时还是需要用nginx配置下载页面.这里就是一个简单的配置nginx下载页面的过程 ...

  6. thinkphp中的内置操作数据库与mysql中的函数汇总

    8.4.4 Model类getModelName() 获取当前Model的名称getTableName() 获取当前Model的数据表名称switchModel(type,vars=array()) ...

  7. mysql连表分组报错---- sql_mode=only_full_group_by问题解决

    #### sql语句报错问题 #1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggreg ...

  8. 【原创】Linux基础之文件编码

    查看 1 vi $ vi $file:set fileencoding 2 file $ file $file 修改 $ vi $file:set fileencoding=utf-8

  9. 【原创】运维基础之Docker(6)性能

    The general result is that Docker is nearly identical to Native performance and faster than KVM in e ...

  10. mysql报ERROR:Deadlock found when trying to get lock; try restarting transaction(nodejs)

    1 前言 出现错误 Deadlock found when trying to get lock; try restarting transaction.然后通过网上查找资料,重要看到有用信息了. 错 ...