python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip
1-Map()
1.1 Syntax
# fun : a function applying to the iterable object
# iterable : such as list, tuple, string and other iterable object
map(fun, *iterable) # * token means that multi iterables is supported
1.2 Working
map() applying the given function to each item of the given iterable object.
map() returns an iterable object called "map object".
1.3 Examples
# eg 1
def addition(n):
return n + n
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
>>>[2,4,6,8]
# eg 2
numbers = (1, 2, 3, 4)
result = map(lambda x: x + x, numbers)
print(list(result))
>>>[2,4,6,8]
# eg 3 - multi iterables
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
result = map(lambda x, y: x + y, numbers1, numbers2)
print(list(result))
>>>[5,7,9]
2-Filter()
2.1 Syntax
# fun : a function that tests if each element of the sequence true or not.
# sequence : who needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.
filter(fun, sequence)
2.2 Working
filter() applying the given function to each item of the given sequence object, and remain the eligible element.
filter() returns an iterator that is already filtered.
2.3 Examples
# eg 1
seq = [0, 1, 2, 3, 5, 8, 13]
result = filter(lambda x: x % 2 == 0, seq)
print(list(result))
>>>[0, 2, 8]
3-Reduce()
3.1 Syntax
# fun : a function applying to all elements of the sequence.
# sequence : who needs to be computered by itself, it can be sets, lists, tuples, or containers of any iterators.
filter(fun, sequence)
3.2 Working
- At first step, first two elements of sequence are picked and the result is obtained.
- Next step is to apply the same function to the previously attained result and the number just succeeding the second element and the result is again stored.
- This process continues till no more elements are left in the container.
- The final returned result is returned and printed on console.
3.3 Examples
from functools import reduce
lis = [1, 3, 5, 6, 2]
print (reduce(lambda a,b : a+b,lis))
print (reduce(lambda a,b : a if a > b else b,lis))
>>>17
>>>6
4-Zip()
4.1 Syntax
zip(*iterators) # * token means that multi iterators is supported
4.2 Working
zip() returns a single iterator object, having mapped values from all the containers.
4.3 Examples
# 1. How to zip the iterators?
name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ]
roll_no = [ 4, 1, 3, 2 ]
marks = [ 40, 50, 60, 70 ]
mapped = list(zip(name, roll_no, marks))
print(mapped)
>>>[('Shambhavi', 3, 60), ('Astha', 2, 70),('Manjeet', 4, 40),('Nikhil', 1, 50)]
# 2. How to unzip?
namz, roll_noz, marksz = zip(*mapped)
# 3. How to traversal them?
players = [ "Sachin", "Sehwag", "Gambhir", "Dravid", "Raina" ]
scores = [100, 15, 17, 28, 43 ]
for pl, sc in zip(players, scores):
print ("Player : %s Score : %d" %(pl, sc))
>>>
Player : Sachin Score : 100
Player : Sehwag Score : 15
Player : Gambhir Score : 17
Player : Dravid Score : 28
Player : Raina Score : 43
python常用函数进阶(2)之map,filter,reduce,zip的更多相关文章
- Python面试题之Python中的lambda map filter reduce zip
当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...
- Python中map,filter,reduce,zip的应用
事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- 【转】Python之函数进阶
[转]Python之函数进阶 本节内容 上一篇中介绍了Python中函数的定义.函数的调用.函数的参数以及变量的作用域等内容,现在来说下函数的一些高级特性: 递归函数 嵌套函数与闭包 匿名函数 高阶函 ...
- Python常用函数记录
Python常用函数/方法记录 一. Python的random模块: 导入模块: import random 1. random()方法: 如上如可知该函数返回一个[0,1)(左闭右开)的一个随机的 ...
- python 常用函数、内置函数清单
文章内容摘自:http://www.cnblogs.com/vamei 1.type() 查询变量的类型 例: >>> a = 10>>> print a10> ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
- 如何在python3.3用 map filter reduce
在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0 ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
随机推荐
- 01 Javascript简介(了解)
Web前端有三层: HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化页面) JavaScript:从交互的角度,描述行为(提升用户体验) JavaScript历史背景介绍 布 ...
- spring_three
转账案例 坐标: ; } } 创建增强类Logger.java /** * 用于记录日志的工具类,它里面提供了公共的代码 */ @Component("logger") @Aspe ...
- C++开发常见问题记录
1.提示strcpy等函数不安全,建议使用strcpy_s等函数. 处理方法:在VS集成环境的 项目->属性->C/C++->预处理器->预处理器定义 中添加 _CRT_SEC ...
- Hive学习之路(一)—— Hive 简介及核心概念
一.简介 Hive是一个构建在Hadoop之上的数据仓库,它可以将结构化的数据文件映射成表,并提供类SQL查询功能,用于查询的SQL语句会被转化为MapReduce作业,然后提交到Hadoop上运行. ...
- angular2最详细的开发环境搭建过程
本文所需要的源代码,从 http://files.cnblogs.com/files/lingzhihua/angular2-quickstart.rar 下载 angular官方推荐使用quicks ...
- 【JVM】02垃圾回收机制
垃圾回收 垃圾回收策略https://blog.csdn.net/u010425776/article/details/51189318 程序计数器.Java虚拟机栈.本地方法栈都是线程私有的,也就是 ...
- jQuery-ajax-.get,.post方法
在上一篇中详细介绍了jQuery中ajax局部方法$.load()的使用.下面来介绍两个全局方法$.get(),$.post()的使用. 1.这两个全局方法其实和上一篇中的$.load()方法使用是差 ...
- .Net之使用Jquery Ajax通过FormData对象异步提交图片文件到服务端保存并返回保存的图片路径
前言: 首先对于图片上传而言,在我们的项目开发中可以说出现的频率是相当的高的.这篇文章中,我将要描述的是在我们.Net中如何使用Jquery Ajax通过FormData对象异步提交图片文件到后台保存 ...
- C# 死锁 Task/AutoResetEvent
与之前<C# 死锁 TaskCompletionSource>类似,还有很多死锁的案例 使用Task异步转同步时,使用不当造成的死锁 private void Task_OnClick(o ...
- SwiftLint:代码规范检查工具介绍
Swift-CodeStyle Checker:SwiftLint 介绍: SwiftLint 是一个用于强制检查 Swift 代码风格和规定的一个工具,基本上以 GitHub's Swift 代码风 ...