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. hdu1495(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:有三个杯子,开始时第一个杯子装满水(体积为a),倒来倒去,得到其中2个杯里的水的体积都为a ...

  2. Egret是一套完整的HTML5游戏开发解决方案

    Egret是一套完整的HTML5游戏开发解决方案.Egret中包含多个工具以及项目.Egret Engine是一个基于TypeScript语言开发的HTML5游戏引擎,该项目在BSD许可证下发布.使用 ...

  3. 三点半们耐热i哦好家哦i囧囧【

    http://pan.baidu.com/share/link?shareid=3011665141&uk=338692646&third=15                http ...

  4. CSDN开源夏令营 百度数据可视化实践 ECharts(8)问题分析

    ECharts问题描写叙述: 问题就是折线图上的点是显示的,有人问能不能一開始不显示,当你点击的时候或者是当鼠标移动到上面的时候,折线上的点才显示? 例如以下图所看到的: 分析:让折线上的点不显示能够 ...

  5. nginx学习12 ngx_cycle_t 和 ngx_init_cycle

    在nginx在启动过程,ngx_init_cycle这个函数最初始工作.变量的初始化存储在ngx_cycle_t这个结构体中,为了深入了解这个函数都做了那些初始化工作,就化时间研究了一下.并写下来以便 ...

  6. Debian7.6安装过程中遇到的问题

    一 sudo命令不能用 1 使用su切换到root用户,命令: su 2 使用名:vim /etc/sudoers加入sudoer用户,命令: vim /etc/sudoers 找到root=(ALL ...

  7. Cocos2d-x中父节点scale对子节点的影响

    背景:在前几天,刚接触cocos2d-x,随便找了一张图,作为一个CCSprite,而且设置了scale属性,然后在这个sprite上创建了一个CCLabelTTF,并用sprite->addC ...

  8. 安装pygame

    pygame的安装 我们首先要去到:http://www.pygame.org/download.shtml 下载我们所需要的软件包: 我选择的是:pygame-1.9.2a0.win32-py3.2 ...

  9. JVM最多支持多少个线程?

    JVM最多支持多少个线程? McGovernTheory在StackOverflow提了这样一个问题: Java虚拟机最多支持多少个线程?跟虚拟机开发商有关么?跟操作系统呢?还有其他的因素吗? Edd ...

  10. linux提取锁和信号灯经常使用

    1.信号( 这两个过程之间的同步) struct semaphore power_sem; sema_init(&pdata->power_sem,1); down(&pdata ...