Demo of Python "Map Reduce Filter"
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"的更多相关文章
- Python基础-map/reduce/filter
一.map Python内置函数,用法及说明如下: class map(object): """ map(func, *iterables) --> map obj ...
- Python: lambda, map, reduce, filter
在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...
- python基础===map, reduce, filter的用法
filter的用法: 这还是一个操作表list的内嵌函数'filter' 需要一个函数与一个list它用这个函数来决定哪个项应该被放入过滤结果队列中遍历list中的每一个值,输入到这个函数中如果这个函 ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- [python基础知识]python内置函数map/reduce/filter
python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...
- Python学习:函数式编程(lambda, map() ,reduce() ,filter())
1. lambda: Python 支持用lambda对简单的功能定义“行内函数” 2.map() : 3.reduce() : 4.filter() : map() ,reduce() , filt ...
- python 函数式编程之lambda( ), map( ), reduce( ), filter( )
lambda( ), map( ), reduce( ), filter( ) 1. lambda( )主要用于“行内函数”: f = lambda x : x + 2 #定义函数f(x)=x+2 g ...
- 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, ...
- python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))
1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...
随机推荐
- tomcat dbcp 基于jndi当配置java.sql.SQLException: Already closed
最近发现了一个现象,观察到的生产环境,不要有一段时间操作,然后另一个操作,首先将有一个数据库连接:java.sql.SQLException: Already closed.,例如下列: error ...
- CF 553A 组合DP
http://codeforces.com/problemset/problem/553/A A. Kyoya and Colored Balls time limit per test 2 seco ...
- Oracle 数据迁移(从Oracle11G迁移到更高的版本号Oracle10G低版本号)
1.数据库状况 生产环境是11G,linux系统,測试环境是10G,windows系统,须要从生产环境导出一个用户下全部的数据,导入測试环境中. 由于数据量比較小,准备採用EXP和IMP工具来做 ...
- Hot Days Codeforces Round #132 (Div. 2) D(贪婪)
Description The official capital and the cultural capital of Berland are connected by a single road ...
- Codeforces Round#310 div2
C题:这题说的是套娃,如果做题的时候知道是套娃,那就好理解多了 规则1:套娃A可以放到套娃B里面,当且仅当套娃B没有放在其他套娃里面 规则2:套娃A放在套娃B里面,且套娃B没有放在其他套娃里面,那么可 ...
- in与exist , not in与not exist 的区别(转)
in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的 ...
- SQL Server :理解GAM和SGAM页
原文:SQL Server :理解GAM和SGAM页 我们知道SQL Server在8K 的页里存储数据.分区就是物理上连续的8个页.当我们创建一个数据库,数据文件会被逻辑分为页和区,当用户对象创建时 ...
- Android源代码下载之《Android新闻client源代码》
介绍 Android新闻client源代码,功能上分为:新闻.关注.读报.微博.里面比較有特色的就是读报功能.真正安装报纸的排版进行读报.给人得感觉就像是在读真实的报纸.事实上即使首页的动态云标签很有 ...
- HDU 4337 King Arthur's Knights 它输出一个哈密顿电路
n积分m文章无向边 它输出一个哈密顿电路 #include <cstdio> #include <cstring> #include <iostream> usin ...
- Java进阶 创建和销毁对象
最近准备写点Javase的东西,希望可以帮助大家写出更好的代码. 1.给不可实例化的类提供私有构造器 比如:每个项目中都有很多工具类,提供了很多static类型的方法供大家使用,谁也不希望看到下面的代 ...