python中的operator.itemgetter函数

来自:https://blog.csdn.net/dongtingzhizi/article/details/12068205

operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号,看下面例子:

import operator

a = [1, 2, 3]
b = operator.itemgetter(1)      # 定义函数b,获取对象的第1个域的值
print(b(a))

结果:2

b = operator.itemgetter(1, 0)   # 定义函数b,获取对象第1个域和第0个的值
print(b(a))

结果:(2,1)

operator.itemgetter函数获取的不是值,而是定义一个函数,通过该函数作用到对象上才能获取值。

sorted函数

sorted(iterable[, cmp[, key[, reverse]]])
  • iterable:可迭代对象
  • cmp:cmp函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。
  • key:key的参数为一个函数或者lambda函数。
  • reverse:reverse = True 降序 , reverse = False 升序(默认)。
a = [(‘john‘, ‘A‘, 15), (‘jane‘, ‘B‘, 12), (‘dave‘, ‘B‘, 10)]
b = sorted(a, key = operator.itemgetter(2))
print(b)

原文地址:https://www.cnblogs.com/keye/p/10425836.html

时间: 2024-08-28 09:11:11

python中的operator.itemgetter函数的相关文章

关于python中的operator.itemgetter()函数的用法

1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2, 3] >>>b = operator.itemgetter(1) >>>print(b) 返回是: >>>operator.itemgetter(1) 也就是说,返回的并不是一个具体的数字,而是一个函数. 再进行如下操作: >>>pr

Python中的sorted函数以及operator.itemgetter函数

operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2,3] >>> b=operator.itemgetter(1)      //定义函数b,获取对象的第1个域的值>>> b(a) 2 >>> b=operator.itemgetter(1,0)  //定义函数b,获取对象的第1个域和第0个的值>&

Python中的sorted函数以及operator.itemgetter函数 【转载】

operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2,3] >>> b=operator.itemgetter(1)      //定义函数b,获取对象的第1个域的值>>> b(a) 2 >>> b=operator.itemgetter(1,0)  //定义函数b,获取对象的第1个域和第0个的值>&

python中operator.itemgetter函数

operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. 1 k = [3,6,8] 2 b = operator.itemgetter(1) 3 print(b(k)) 4 #输出6 1 k = [3,6,8] 2 b = operator.itemgetter(2,0) 3 print(b(k)) 4 #输出(8, 3) 要注意,operator.itemgetter函数获取的不是值,而是定义了一个函数,通过

python中的内置函数getattr()

在python的官方文档中:getattr()的解释如下: getattr(object, name[, default]) Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For examp

函数式编程 &amp; Python中的高阶函数map reduce filter 和sorted

1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数式编程的第一型.在面向对象编程中,我们把对象传来传去,那在函数式编程中,我们要做的是把函数传来传去,而这个,说成术语,我们把他叫做高阶函数.飞林沙 2)特点 计算视为视为函数而非指令 纯函数式编程:不需变量,无副作用,测试简单(每次的执行结果是一样的) 支持高阶函数,代码简洁 2. python支持

Python学习(二):入门篇:python中流程控制与函数编写

python中流程控制与函数编写 Last Eidt 2014/5/2 转载请注明出处http://blog.csdn.net/jxlijunhao 一,流程控制 1)布尔逻辑 Python中利用True来表示逻辑真,False来逻辑假 not :非 and:与 or   :或 ==  :逻辑等 >>> False==True False >>> False==False True >>> not False True >>> Fal

python中enumerate()函数用法

python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出, 2.将 list 倒序成 [6, 5, 4, 3, 2, 1] 3.将a 中的偶数挑出 *2 ,结果为 [4, 8, 12] 这个例子用到了python中enumerate的用法.顺便说一下enumerate在for循环中得到计数的用法,enumerate参数为可遍历的变量,如 字符串,列表等

Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str()或者 repr() . >>> a = 10 >>> type(str(a)) <class 'str'> >>> type(repr(a)) <class 'str'> 但是这二者之间有什么区别呢?因