Here I share with you a demo for python map, reduce and filter functional programming thatowned by me(Xiaoqiang).




I assume there are two DB tables, that `file_logs` and `expanded_attrs` which records more columns to expand table `file_logs`. For demonstration, we assume that there are more than one file logs for a same tuple of (platform_id, client_id). We need to feture
out which is the one lasted updated for (platform_id=1, client_id=1) tuple.



Here is the thoughts:



1. Filter out all file logs for tuple (platform_id=1, client_id=1) from original file logs,

2. Merge expand table attributes into file_logs table in memory, like union selection.

3. Reduce the full version of file_logs for figuring out which is latest updated.



Demo codes shows here (use Python 2.6+, 2.7+):

BTW, you are welcome if you feature out a more effective way of working or any issues you found. Thanks. :)

#!/usr/bin/env python

"""
Requirement:
known platform_id=1, client_id=1 as pid and cid.
exists file_logs and expanded_attrs which are array of objects, expanded_attrs is a table of columns expand table file_logs
as file_logs contains more than one for pid=1,cid=1, we need to find out which is the one latest updated.
""" file_logs = [
{ 'file_log_id': '1', 'platform_id': '1', 'client_id': '1', 'file': 'path/to/platform/client/j-1/stdout' },
{ 'file_log_id': '2', 'platform_id': '1', 'client_id': '1', 'file': 'path/to/platform/client/j-2/stdout' },
{ 'file_log_id': '3', 'platform_id': '2', 'client_id': '3', 'file': 'path/to/platform/client/j-3/stdout' },
] expanded_attrs = [
{ 'file_log_id': '1', 'attr_name': 'CLICK', 'attr_value': '100' },
{ 'file_log_id': '1', 'attr_name': 'SUPPRESSION', 'attr_value': '100' },
{ 'file_log_id': '1', 'attr_name': 'last_updated', 'attr_value': '2014-07-14' },
{ 'file_log_id': '2', 'attr_name': 'CLICK', 'attr_value': '200' },
{ 'file_log_id': '2', 'attr_name': 'SUPPRESSION', 'attr_value': '200' },
{ 'file_log_id': '2', 'attr_name': 'last_updated', 'attr_value': '2014-07-15' },
{ 'file_log_id': '3', 'attr_name': 'CLICK', 'attr_value': '300' },
{ 'file_log_id': '3', 'attr_name': 'SUPPRESSION', 'attr_value': '300' },
{ 'file_log_id': '3', 'attr_name': 'last_updated', 'attr_value': '2014-07-15' },
] platform_id = '1'
client_id = '1' target_scope_filelogs = filter(lambda x: x['platform_id'] == platform_id and x['client_id'] == client_id, file_logs) map(
lambda x:
x.update(reduce(
lambda xx, xy: xx.update({ xy['attr_name']: xy['attr_value'] }) is None and xx,
filter(lambda xx: xx['file_log_id'] == x['file_log_id'], expanded_attrs),
dict()
)),
target_scope_filelogs
) print reduce(lambda x, y: x['last_updated'] > y['last_updated'] and x or y, target_scope_filelogs)
#> {'file_log_id': '2', 'platform_id': '1', 'last_updated': '2014-07-15', 'SUPPRESSION': '200', 'file': 'path/to/platform/client/j-2/stdout', 'client_id': '1', 'CLICK': '200'}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Demo of Python "Map Reduce Filter"的更多相关文章

  1. Python基础-map/reduce/filter

    一.map Python内置函数,用法及说明如下: class map(object): """ map(func, *iterables) --> map obj ...

  2. Python: lambda, map, reduce, filter

    在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...

  3. python基础===map, reduce, filter的用法

    filter的用法: 这还是一个操作表list的内嵌函数'filter' 需要一个函数与一个list它用这个函数来决定哪个项应该被放入过滤结果队列中遍历list中的每一个值,输入到这个函数中如果这个函 ...

  4. python之map、filter、reduce、lambda函数 转

    python之map.filter.reduce.lambda函数  转  http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...

  5. [python基础知识]python内置函数map/reduce/filter

    python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...

  6. Python学习:函数式编程(lambda, map() ,reduce() ,filter())

    1. lambda: Python 支持用lambda对简单的功能定义“行内函数” 2.map() : 3.reduce() : 4.filter() : map() ,reduce() , filt ...

  7. python 函数式编程之lambda( ), map( ), reduce( ), filter( )

    lambda( ), map( ), reduce( ), filter( ) 1. lambda( )主要用于“行内函数”: f = lambda x : x + 2 #定义函数f(x)=x+2 g ...

  8. Python map/reduce/filter/sorted函数以及匿名函数

    1. map() 函数的功能: map(f, [x1,x2,x3]) = [f(x1), f(x2), f(x3)] def f(x): return x*x a = map(f, [1, 2, 3, ...

  9. python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))

    1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...

随机推荐

  1. zoj3747(递推dp)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5170 题意:给n个士兵排队,每个士兵三种G.R.P可选,求至少有m个 ...

  2. umlの实现图

    在uml中大部分模型描写叙述了逻辑和设计方面的信息: 用例图知道期望 类图能够知道问题域的词汇(类.对象) 状态图.交互图和活动图能够知道类图中的词汇是怎样写作完毕行为的(逻辑结构) 实现图是用来描写 ...

  3. Android 网络通信框架Volley基本介绍

    Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...

  4. Java集群--大型网站是怎样解决多用户高并发访问的

    时间过得真快,再次登录博客园来写博,才发现距离上次的写博时间已经过去了一个月了,虽然是因为自己找了实习,但这也说明自己对时间的掌控能力还是没那么的强,哈哈,看来还需不断的努力啊!(这里得特别说明一下本 ...

  5. Intel baytrail-t support Linux?

    点击这里查看文章 有空试试---唉... Ubuntu (Linux) on Atom Z3700 Series ASUS Transformer Book T100 is particularly ...

  6. 通过angularjs的directive以及service来实现的列表页加载排序分页(转)

    前两篇:(列表页的动态条件搜索,我是如何做列表页的)分别介绍了我们是如何做后端业务系统数据展示类的列表页以及动态搜索的,那么还剩下最重要的一项:数据展示.数据展示一般包含三部分: 数据列头 数据行 分 ...

  7. 正确Linux新手很实用20命令

     //正确Linux新手很实用20命令 //slwang  2014.4.19 1, ls list directory contents 内容 ls -l     //以详情模式(long li ...

  8. linux下Ftp环境的搭建

      Ftp环境的搭建 1.ftp软件的安装 使用ssh远程连接linux系统,上传和下载一些文件,Ftp是不可少的 Ftp的安装很简单,远程登录系统后使用命令 yum list vsftpd 通过提示 ...

  9. 小米2S TWRP 3.0.2-0 最新版Recovery

    主界面 使用了我最新修改的内核 下载地址: 链接: http://pan.baidu.com/s/1i5xwddb 密码: 7dyb 验证信息: md5sum: dca410f33020eb87986 ...

  10. Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)

    集合操作符UNION/UNION ALLINTERSECTMINUS Union All不排序,不去重,其余均升序且去重.create table e1 as select * from emp wh ...