一、介绍

operator模块导出一组与Python的内部运算符相对应的高效函数。例如,等同于表达式。函数名称是用于特殊类方法的函数名称; 为方便起见,还提供了没有前导和尾随的变体。operator.add(x, y)x+y__

这些函数属于执行对象比较,逻辑运算,数学运算,序列运算和抽象类型测试的类别。

模块特殊函数:都返回一个可调用对象,可以用于 map(), sorted(), itertools.groupby() 等接受一个函数对象作为参数的函数

operator.attrgetter(*attrs):返回对象的对应属性
operator.itemgetter(*items):返回对象对应位置的值,相当于 __getitem__() 方法
operator.methodcaller(name[, args...]):返回对象的对应方法函数,额外输入的 args 会作为此方法的默认参数

二、代码

import operator

a = [1, 2, 3, 4, 5]
b = operator.itemgetter(1)
print(b(a)) e = {'E': 1, 'A': 2, 'B': 1, 'D': 3, 'C': 1}
res = sorted(e.items(), key=operator.itemgetter(1), reverse=True)
print(res) students = [
{"name": "maple", "age": 22, "score": 92},
{"name": "ffm", "age": 19, "score": 99},
{"name": "angel", "age": 23, "score": 82},
{"name": "tom", "age": 20, "score": 88},
{"name": "jerry", "age": 22, "score": 95}
] students_score = sorted(students, key=operator.itemgetter("score"), reverse=True)
print(students_score) class Student:
def __init__(self, name, id):
self.name = name
self.id = id def __repr__(self):
return "(%s, %s)" % (self.name, self.id) students = [Student('maple', 167), Student('ffm', 288), Student('angel', 134), Student('tom', 111), Student('jerry', 101)] students_by_id = sorted(students, key=operator.attrgetter('id'))
print(students_by_id)
#代码结果
2
[('D', 3), ('A', 2), ('E', 1), ('B', 1), ('C', 1)]
[{'name': 'ffm', 'age': 19, 'score': 99}, {'name': 'jerry', 'age': 22, 'score': 95}, {'name': 'maple', 'age': 22, 'score': 92}, {'name': 'tom', 'age': 20, 'score': 88}, {'name': 'angel', 'age': 23, 'score': 82}]
[(jerry, 101), (tom, 111), (angel, 134), (maple, 167), (ffm, 288)]

python模块operator对排序的辅助功能的更多相关文章

  1. Python list列表的排序

    当我们从数据库中获取一写数据后,一般对于列表的排序是经常会遇到的问题,今天总结一下python对于列表list排序的常用方法: 第一种:内建函数sort() 这个应该是我们使用最多的也是最简单的排序函 ...

  2. python的operator.itemgetter('click')用于定义获取'click'项的函数

    python的排序参见文章http://blog.csdn.net/longshenlmj/article/details/12747195 这里介绍 import operator模块 operat ...

  3. Python的operator.itemgetter函数和sorted函数

    写这篇文章的目的是之前在<机器学习实战>用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...

  4. python中map的排序以及取出map中取最大最小值

    map排序: 1.按key排序: items=dict.items() items.sort() sorted(dict.items(),key=lambda x:x[0],reverse=False ...

  5. python sort 和sorted排序

    当我们从数据库中获取一写数据后,一般对于列表的排序是经常会遇到的问题,今天总结一下python对于列表list排序的常用方法: 第一种:内建方法sort() 可以直接对列表进行排序 用法: list. ...

  6. Python模块学习

    6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...

  7. python-学习笔记之-Day5 双层装饰器 字符串格式化 python模块 递归 生成器 迭代器 序列化

    1.双层装饰器 #!/usr/bin/env python # -*- coding: utf-8 -*- # author:zml LOGIN_INFO = False IS_ADMIN = Fal ...

  8. Python模块操作

    Exceptions 模块 该模块定义了以下标准异常: • Exception 是所有异常的基类. 强烈建议(但不是必须)自定义的异常异常也继承这个类. • SystemExit(Exception) ...

  9. 【转】利用Boost.Python将C++代码封装为Python模块

    用Boost.Python将C++代码封装为Python模块 一.     基础篇 借助Boost.Python库可以将C/C++代码方便.快捷地移植到python模块当中,实现对python模块的扩 ...

随机推荐

  1. utmp, wtmp - 登 录 记 录(login records)

    SYNOPSIS[总览] #include DESCRIPTION[描述] utmp 文 件 用 于 记 录 当 前 系 统 用 户 是 哪 些 人. 但 是 实 际 的 人 数 可 能 比 这 个 ...

  2. eclipse创建MAVEN项目是出现Could not resolve archetype的解决办法

    eclipse第一次创建MAVEN项目时出现这个问题,查了很多文档改了没用,后来问了别人知道是maven中央仓库下载插件包失败就会报错. 解决办法: 用国内阿里云镜像会好很多 在settings.xm ...

  3. 搜索进阶课件,视频,代码(状态压缩搜索,折半搜索,dfs,bfs总结)

    链接:https://pan.baidu.com/s/1-svffrprCOO4CtQoCTQ9hQ 提取码:h909 复制这段内容后打开百度网盘手机App,操作更方便哦

  4. Wpf 获取指定字体和大小的字符的长宽

    Wpf 获取指定字体和大小的字符的长宽 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-09 参考: 章节: 比如一个 Consolas 字体 ...

  5. sublime快捷键汇总

    Sublime Text 3 快捷键精华版 Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所 ...

  6. CF 398 E(动态规划)

    传送门: http://codeforces.com/problemset/problem/398/E 题解: 首先答案不超过2. 最长环=1时,ans=0 最长环=2时,ans=1 否则,ans=2 ...

  7. NX二次开发-UFUN求对象的最大边界框UF_MODL_ask_bounding_box

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...

  8. NX二次开发-对话框加锁UF_UI_lock_ug_access

    VC/MFC调用UG Dialog要进入加锁状态 加锁 UF_UI_lock_ug_access ( UF_UI_FROM_CUSTOM ); 此处为UF_UI_select的函数 解锁 UF_UI_ ...

  9. Linux命令(2):cat

    cat命令是一个文本连接和查看的命令,用于文件的输出显示. 三大功能 一次显示整个文件. $ cat filename 从键盘创建一个文件.只能创建新文件,不能编辑已有文件. $ cat > f ...

  10. 其它课程中的python---1、python基础

    其它课程中的python---1.python基础 一.总结 一句话总结: 可以先把视频平台搭起来,这样学习效率会高很多,而且有额外收益 1.python的优势有哪些? 一个广泛的标准库 扩展性:比如 ...