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. poj2387(最短路)

    题目连接:http://poj.org/problem?id=2387 题意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离. 分析:最短路裸题. #include ...

  2. 阿里云免费试用之体验——阿里云serverECS试用心得

    自上次參加了阿里云的开发人员大会回来 心里就一直惦记着阿里云 由于曾经各种各样什么的server也用了不少 年前開始接触阿里云 一直没有给予很多其它的关注 參加了这次的开发人员大会后 就想更进一步的了 ...

  3. [置顶] ※数据结构※→☆线性表结构(list)☆============双向链表结构(list double)(三)

    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点. ~~~~~~~~~~~~ ...

  4. Android中callback(接口回调)机制

    事实上,callback 机制在Android 中无处不在,特别是以Handler.Callback.Listener这三个词结尾的,都是利用callback机制来实现的.比方点击事件onClickL ...

  5. hadoop日志分析

    一.项目要求 本文讨论的日志处理方法中的日志,仅指Web日志.事实上并没有精确的定义,可能包含但不限于各种前端Webserver--apache.lighttpd.nginx.tomcat等产生的用户 ...

  6. 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030

    中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030 cp936是微软自己发布的用在文件系统中的编码方式.而bg2312是中国国家标准.我明白mount -t vfa ...

  7. POJ 2182 Lost Cows(牛排序,线段树)

    Language: Default Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9207   Acce ...

  8. 学习NodeJS第一天:node.js介绍

    Node.JS 前辈 C 程序猿 Ryan Dahl(http://four.livejournal.com/)工程,根据 Google 著名的开源 JavaScript 发动机 V8 对于二次开发 ...

  9. java它们的定义ArrayList序列, 大神跳跃

    一个list有两种类型的对象,今天有需求必须责令不同的约会对象,这里是代码 /** *@author xh1991101@163.com */ List<Message> messages ...

  10. 赤裸裸的splay平衡树

    HYSBZ1588 http://www.lydsy.com/JudgeOnline/problem.php?id=1588 给我们n天的营业额, 要求出每天的最小波动值,然后加起来.  当天最小波动 ...