写这篇文章的目的是之前在《机器学习实战》用Python3实现KNN算法时用到的几个函数不太懂,

地址:

1- https://github.com/hitergelei/Self-Learning/blob/master/Machine Learning/Machine Learning in Action/2_KNN.py

2-https://github.com/apachecn/MachineLearning/blob/master/src/python/2.KNN/kNN.py

所以相关函数用法在这里做一下解析:

  • Python的operator.itemgetter函数

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

In [10]: import operator

In [11]:  a = [1,2,3]

In [12]:  b = operator.itemgetter(1) //定义函数b,获取对象的第1个域的值

In [13]: b(a)
Out[13]: 2 In [14]: c = operator.itemgetter(2,1) //定义函数b,获取对象的第2个域和第1个的值 In [15]: c(a)
Out[15]: (3, 2)

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

  • Python的sorted函数

sorted() 函数对所有可迭代的对象进行排序操作。

sort 与 sorted 区别:

1- sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。

2- list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。

实例:

In [20]:  a= [5,7,6,4,3,21,1,2]

In [21]: b = sorted(a)  #保留原列表a,返回一个新列表b

In [22]: a
Out[22]: [5, 7, 6, 4, 3, 21, 1, 2] #原列表a不变 In [23]: b
Out[23]: [1, 2, 3, 4, 5, 6, 7, 21] #新列表b

sorted 语法:

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

参数说明:

(1) iterable :可迭代对象。

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

即:cmp(x,y) 函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。

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



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

(注:python3 sorted取消了对cmp的支持。 )



如果我们想用第二个关键字排过序后再用第一个关键字进行排序呢?



补充另一种方法:

sorted(M, key = operator.itemgetter(1,0))

先根据第2个域排序,再根据第1个域排序

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

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

sorted(students, key=operator.itemgetter(2)) 默认reverse为false

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

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

即先根据第二个域排序,再根据第三个域排序。

(4) reverse :是一个bool变量,表示升序还是降序排列,默认为false(升序排列),定义为True时将按降序排列。

sorted(students, key=operator.itemgetter(2),reverse = True)

sorted(students, key=operator.itemgetter(1),reverse = True)

sorted(students, key=operator.itemgetter(1,2),reverse = True)

Python的operator.itemgetter函数和sorted函数的更多相关文章

  1. python学习 -- operator.itemgetter(), list.sort/sorted 以及lambda函数

    Python 中有非常方便高效的排序函数,下面主要介绍如何sort/sorted对list,dict进行排序. 1. 用list.sort /sorted 对list of tuples中第二个值进行 ...

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

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

  3. python之zip函数和sorted函数

    # zip()函数和sorted()函数 # zip()函数:将两个序列合并,返回zip对象,可强制转换为列表或字典 # sorted()函数:对序列进行排序,返回一个排序后的新列表,原数据不改变 # ...

  4. python中operator.itemgetter函数

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

  5. python中operator.itemgetter

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

  6. reversed()函数和sorted()函数

    #reversed()反转排序,可对列表.元组.区间等进行排序 #练习1 a = range(10) a_list = [x for x in reversed(a)] print(a_list) # ...

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

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

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

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

  9. Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函数 os模块 hashlib模块 platform模块 csv模块

    Python第十一天    异常处理  glob模块和shlex模块    打开外部程序和subprocess模块  subprocess类  Pipe管道  operator模块   sorted函 ...

随机推荐

  1. python3----基础函数的参数是可变参数,将传进来的参数转成列表

    def myFun(*argments): values = [x for x in argments] print(values) myFun(1,2,3,4,5,6) result: [1, 2, ...

  2. TypeError: write() argument must be str, not bytes

    w文件打开以 '二进制'  方式: with open('teacher.html','wb+') as f: f.write(response.body) 要写入"中文",防止乱 ...

  3. NoSQL-MongoDB with python

    前言: MongoDB,文档存储型数据库(document store).NoSQL数据库中,它独占鳌头,碾压其他的NoSQL数据库. 使用C++开发的,性能仅次C.与redis一样,开源.高扩展.高 ...

  4. C++ 基础知识回顾(string基础、智能指针、迭代器、容器类)

    [1] string基础 [1.1] string 的构造 #include <iostream> #include <string> int main() { using n ...

  5. 《从零开始学Swift》学习笔记(Day 17)——Swift中数组集合

    原创文章,欢迎转载.转载请注明:关东升的博客 数组(Array)是一串有序的由相同类型元素构成的集合.数组中的集合元素是有序的,可以重复出现. 声明一个Array类型的时候可以使用下面的语句之一. v ...

  6. 大型软件公司.net面试题

    1:a=10,b=15,在不用第三方变量的前提下,把a,b的值互换   2:已知数组int[] max={6,5,2,9,7,4,0};用快速排序算法按降序对其进行排列,并返回数组   3:请简述面向 ...

  7. Event Scheduler

    MySQL :: MySQL 5.7 Reference Manual :: 23.4 Using the Event Scheduler https://dev.mysql.com/doc/refm ...

  8. css选择器的权重

    权重会叠加!

  9. CF145E Lucky Queries

    CF145E Lucky Queries 英文题面不放了,直接上翻译: 题目描述 给你n个数,每个数是4或者7,给你m个任务完成 switch l r 把[l,r]位置的4换成7,7换成4 count ...

  10. MySQL中有关icp mrr和bka的特性

    文辉考我的问题,有关这三个的特性,如果在面试过程中,个人见解可以答以下 icp MyQL数据库会在取出索引的同时,判断是否进行WHERE条件过滤,也就是把WHERE的部分过滤操作放在存储引擎层,在某些 ...