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
  1. At first step, first two elements of sequence are picked and the result is obtained.
  2. 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.
  3. This process continues till no more elements are left in the container.
  4. 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的更多相关文章

  1. Python面试题之Python中的lambda map filter reduce zip

    当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...

  2. Python中map,filter,reduce,zip的应用

    事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...

  3. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  4. 【转】Python之函数进阶

    [转]Python之函数进阶 本节内容 上一篇中介绍了Python中函数的定义.函数的调用.函数的参数以及变量的作用域等内容,现在来说下函数的一些高级特性: 递归函数 嵌套函数与闭包 匿名函数 高阶函 ...

  5. Python常用函数记录

    Python常用函数/方法记录 一. Python的random模块: 导入模块: import random 1. random()方法: 如上如可知该函数返回一个[0,1)(左闭右开)的一个随机的 ...

  6. python 常用函数、内置函数清单

    文章内容摘自:http://www.cnblogs.com/vamei 1.type() 查询变量的类型 例: >>> a = 10>>> print a10> ...

  7. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  8. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

  9. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

随机推荐

  1. win10 下的YOLOv3 训练 wider_face 数据集检测人脸

    1.数据集下载 (1)wider_face 数据集网址为 http://shuoyang1213.me/WIDERFACE/index.html 下载以上几项文件(这里推荐 google Drive ...

  2. Python基础,day1

    一. Python介绍 目前Python主要应用领域: 云计算: 云计算最火的语言, 典型应用OpenStack WEB开发: 众多优秀的WEB框架,众多大型网站均为Python开发,Youtube, ...

  3. 【Zookeeper02】ZK的作用以及使用

    上一篇介绍了ZK的安装以及集群的搭建,这只能算是个软件安装过程,具体是做什么的.怎么用也没有做解释,这一篇中博主就自己的私人理解简单写一下: 1.是什么: a.Zookeeper是一个分布式协调服务, ...

  4. 手动搭建自己的nuget服务器及使用

    这篇文章的主要目的: 1.搭建自己的私有的nuget服务器 2.打包代码为nuget包 3.在其他项目中使用私有服务器上的nuget包 一. 搭建自己的nuget服务器 1. 创建一个空的ASP.NE ...

  5. hexo + next搭建博客

    使用hexo和next主题搭建GitHub博客 安装node.js 在node.js 官网下载, 相应系统位数的软件包. 可以选择LTS长期支持版, 或者Current当前版本. 我这里选择的是Win ...

  6. ECShop安装问题

    Ecshop安装过程中的的问题:cls_image::gd_version()和不支持JPEG 在安装Ecshop的时候,遇到两个问题: 1.Strict Standards: Non-static ...

  7. (转)Java 8 中的 Streams API 详解

    为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...

  8. Ajax探讨

    Ajax是一种技术方案,并不是什么新技术,Ajax请求使用XmlHttpRequest对象发送, XmlHttpRequest是一个浏览器接口,使得Javascript可以进行HTTP(S)通信. 最 ...

  9. 《ElasticSearch6.x实战教程》正式推出(附图书抽奖)

    经过接近1个月的时间,ElasticSearch6.x实战教程终于成册.这本实战教程小册有很多不足(甚至可能有错误),也是第一次完整推出一个系列的教程. 1年前,我开始真正接触ES,在此之前仅停留在知 ...

  10. MyBatis从入门到精通:select用法进一步讲解

    selectAll:笔记 /* 定义接口方法的返回值的时候,必须注意查询SQL可能返回的结果数量.当 返回值最多只有一个结果的时候,可以将结果返回值定义为SysUser,此时 返回值类型为List&l ...