python的排序参见文章http://blog.csdn.net/longshenlmj/article/details/12747195

这里介绍 import operator模块

operator的itemgetter函数用于获取传入参数中某个域的值,如

a = [1,2,3] 

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

>>> b(a) 



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

>>> b(a) 

(2, 1)

operator.itemgetter是定义了一个函数,然后利用该函数作用到指定对象上,来获取对应域的值。

b = operator.itemgetter(1)  :定义函数b,用于获取传入的list第1域的值

可以将b用于sort函数的key。作为排序的依据。

adn_app_data_map是个字典

for key, app_arr in adn_app_data_map.items():

app_arr.sort(key=operator.itemgetter('click'), reverse=True)

            app_arr = app_arr[:3]

            keys = key.split('#')

            category = keys[0]

            ad_network_id = keys[1]

            ad_id = keys[2]

            for app in app_arr:

                dimension_values = category + '#' + app['app_id'] + '#' + ad_network_id + '#' + ad_id

                record = app['campaign_id'] + ',' + adn_ad_category_app_report + ',' + dimension_values + ',' + app['impression'] + \

                         ',' + str(app['click']) + ',' + app['impression_cost'] + ',' + app['click_cost'] + '\n'

                csv.writelines(record)

测试如下:

>>> import types

>>> test={'a':'1','b':'2','c':'3','d':'4'}

>>> print test.items()

[('a', '1'), ('c', '3'), ('b', '2'), ('d', '4')]

>>> for key,val in test.items():

    print type(val); 

<type 'str'>

<type 'str'>

<type 'str'>

<type 'str'>

这说明

python的operator.itemgetter('click')用于定义获取'click'项的函数的更多相关文章

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

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

  2. python中operator.itemgetter函数

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

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

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

  4. python中operator.itemgetter

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

  5. PyQt(Python+Qt)学习随笔:QListWidget获取指定项对应行的row方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListWidget的row方法通过项作为参数,获取到对应项所在行的行号,语法如下: int ro ...

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

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

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

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

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

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

  9. Python学习手册之数据封装、类方法、静态方法和属性函数

    在上一篇文章中,我们介绍了 Python 的内部方法.操作符重载和对象生命周期,现在我们介绍 Python 的数据封装.类方法.静态方法和属性函数.查看上一篇文章请点击:https://www.cnb ...

随机推荐

  1. Linux基本知识总结

    一.Linux的基本介绍 起源:大家知道先有Unix,后有的linux就行了,其他的细节可以自己查阅资料. 特点:开源!!! 安全(Linux的病毒远少于window). 免费(商业公司最喜欢这一点) ...

  2. SpringMVC 环境搭建(HelloWorld)

    SpringMVC 入门案例 环境 * springMVC * web 构建文件目录结构 修改dispatcher-servlet.xml文件 修改web.xml

  3. php+xdebug远程调试(单人)

    目录 服务器上安装 XDebug 及配置 客户端 PHPstorm 配置 浏览器安装插件 服务器上安装 XDebug 及配置 XDebug 安装 略 配置: 打开 php.ini 配置文件: vim ...

  4. Architecture : Describable Command and Identifiable Data

    Architecture : Describable Command and Identifiable Data Description Terms Command A command is a fu ...

  5. electron-vue 初体验

    注意事项 首先确保node和npm是最新版本 避免使用镜像(我淘宝镜像安装有报错现象) 避免window的一些坑 若上一项检查完成,我们可以继续设置所需的构建工具.使用 windows-build-t ...

  6. springboot解决跨域问题(Cors)

    1.对于前后端分离的项目来说,如果前端项目与后端项目部署在两个不同的域下,那么势必会引起跨域问题的出现. 针对跨域问题,我们可能第一个想到的解决方案就是jsonp,并且以前处理跨域问题我基本也是这么处 ...

  7. admin的配置

    当我们访问http://127.0.0.1:8080/admin/时,会出现: 执行命令: 生成同步数据库的脚本:python manage.py makemigrations             ...

  8. J-Link固件烧录以及使用J-Flash向arm硬件板下载固件程序

    这篇文章的最初版本是在15年写的https://blog.csdn.net/u010592722/article/details/45575663,后来又遇到了一些新问题,故更新在了这里. 一.始于安 ...

  9. Bootstrap3 表格-带边框的表格

    添加 .table-bordered 类为表格和其中的每个单元格增加边框. <table class="table table-bordered"> ... </ ...

  10. 使用Spring Boot开发Web项目

    前面两篇博客中我们简单介绍了Spring Boot项目的创建.并且也带小伙伴们来DIY了一个Spring Boot自动配置功能,那么这些东西说到底最终还是要回归到Web上才能体现出它的更大的价值,so ...