operator.itemgetter函数

operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子。

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

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

sorted函数
Python内置的排序函数sorted可以对list或者iterator进行排序,官网文档见:http://docs.python.org/2/library/functions.html?highlight=sorted#sorted,该函数原型为:

sorted(iterable[, cmp[, key[, reverse]]])

参数解释:

(1)iterable指定要排序的list或者iterable,不用多说;

(2)cmp为函数,指定排序时进行比较的函数,可以指定一个函数或者lambda函数,如:

students为类对象的list,没个成员有三个域,用sorted进行比较时可以自己定cmp函数,例如这里要通过比较第三个数据成员来排序,代码可以这样写:

      students = [('john', 'A', ), ('jane', 'B', ), ('dave', 'B', )]
sorted(students, key=lambda student : student[])

(3)key为函数,指定取待排序元素的哪一项进行排序,函数用上面的例子来说明,代码如下:
      sorted(students, key=lambda student : student[2])

key指定的lambda函数功能是去元素student的第三个域(即:student[2]),因此sorted排序时,会以students所有元素的第三个域来进行排序。

有了上面的operator.itemgetter函数,也可以用该函数来实现,例如要通过student的第三个域排序,可以这么写:

sorted(students, key=operator.itemgetter()) 

sorted函数也可以进行多级排序,例如要根据第二个域和第三个域进行排序,可以这么写:

sorted(students, key=operator.itemgetter(,))

即先根据第二个域排序,再根据第三个域排序。
(4)reverse参数就不用多说了,是一个bool变量,表示升序还是降序排列,默认为false(升序排列),定义为True时将按降序排列。

operator.itemgetter的用法【转】的更多相关文章

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

    1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2 ...

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

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

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

    operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...

  4. python中operator.itemgetter

    直接上例子: rs=  [...     {...       "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",...  ...

  5. python 字典列表排序operator.itemgetter()

    举例: import operator x = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] sorted_x = sorted(x, ...

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

    operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...

  7. python--sorted函数和operator.itemgetter函数

    1.operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1 ...

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

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

  9. python中的operator.itemgetter函数

    operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号.看下面的例子 a = [,,] >>> b=) ...

随机推荐

  1. 剑指offer--面试题14--收获

    按照作者的说法,作为应届毕业生的我来说,如果能写出初级程序员的参考代码来解决面试题14就可认为过关了... 参考代码如下: void ReorderOddEven_1(int *pData, unsi ...

  2. 01-08-01【Nhibernate (版本3.3.1.4000) 出入江湖】NHibernate中的三种状态

    以下属于不明来源资料: 引入 在程序运行过程中使用对象的方式对数据库进行操作,这必然会产生一系列的持久化类的实例对象.这些对象可能是刚刚创建并准备存储的,也可能是从数据库中查询的,为了区分这些对象,根 ...

  3. Ignore files which are already versioned

    If you accidentally added some files which should have been ignored, how do you get them out of vers ...

  4. Bad configuration option localCommand

     command-line: line 0: Bad configuration option: PermitLocalCommand 2011-12-08 14:04:54 标签:Bad confi ...

  5. hdoj 1596 find the safest road

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...

  6. zoj 3232 It's not Floyd Algorithm(强联通分量,缩点)

    题目 /******************************************************************/ 以下题解来自互联网:Juny的博客 思路核心:给你的闭包 ...

  7. JSP include标签和include指令

    test1.jsp <% int a = 5; out.println(a); %> test2.jsp <jsp:include page="/test1.jsp&quo ...

  8. Oracle日期函数

    Oracle日期函数用于对Oracle数据库中的日期及时间进行处理. (1)ADD_MONTHS Oracle日期函数返回一个具有与所提供日期相差月份的日期,函数中给出了未来或以前的月份数.语法如下: ...

  9. 表单验证插件——validate

    表单验证插件——validate 该插件自带包含必填.数字.URL在内容的验证规则,即时显示异常信息,此外,还允许自定义验证规则,插件调用方法如下: $(form).validate({options ...

  10. HDU2521反素数

    只是了解下这种简单的数论定义,解释可以戳这个 http://www.cnblogs.com/Findxiaoxun/p/3460450.html ,然后按Ctrl+ F搜索   反素数  ,找到那一部 ...