Python 高阶函数map(),filter(),reduce()

map()函数,接收两个参数,一个是函数,一个是序列,map()把传入的函数依次作用于序列的每个元素,并把结果作为新的序列返回;

aa = [1, 2, 3, 4, 5]
print("map-result = ", list(map(lambda a: a * a, aa)))
#map-result =  [1, 4, 9, 16, 25]

filter()函数,接收两个参数,一个是函数,一个是序列,filter()把传入的函数依次作用于每个元素,根据返回值是True还是False决定是保留还是丢弃该元素,结果序列是所有返回值为True的子集;

aa = [1, 2, 3, 4, 5]
print("filter-result = ", list(filter(lambda a: a >=3, aa)))
#filter-result =  [3, 4, 5]

reduce()函数,把一个函数作用于一个序列上,这个函数必须接收两个参数,其中reduce()函数把结果继续和序列的下一个元素做累积计算,reduce()函数只返回值结果中,而非序列。在Python3中,reduce()函数移到了functools包中。

from functools import reduce
aa= [1,2,3,4,5]
print("reduce-result = ",reduce(lambda a, b: (a * b),aa))
#reduce-result =  120

原文地址:https://www.cnblogs.com/zhaoshizi/p/9165255.html

时间: 2024-07-30 19:44:48

Python 高阶函数map(),filter(),reduce()的相关文章

python 高阶函数 map()和reduce()

一.map()函数 map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. 1 >>> from collections import Iterator 2 >>> def f(x): 3 ... return x * x 4 ... 5 >>> r = map(f, [1, 2, 3, 4, 5]) 6 >>> r 7 <map obj

Python 函数式编程--高阶函数Map、Reduce、Filter、Sorted

1.1   高阶函数 变量可指向函数 >>> abs(-10) 10 >>> x = abs    --x指向abs函数 >>> x(-1)      --直接调用x 1 调用abs和调用x完全相同. 函数名也是变量 >>> abs = 10 >>> abs(-10) Traceback (most recent call last): File "<stdin>", line 1,

python 高阶函数map/reduce

Python内建了map()和reduce()函数. map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. 1 def f(x): 2 return x*x 3 r = map(f, [1,2,3,4,5,6]) 4 print(r) 5 print(list(r)) Output: <map object at 0x00000269FA005E10> [1, 4, 9, 16, 25, 36] re

python 高阶函数三 filter()和sorted()

一.filter()函数 filter()接收一个函数和一个序列.filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素. 1 >>> from collections import Iterator 2 >>> def is_odd(n): 3 ... return n % 2 == 1 4 ... 5 >>> it = filter(is_odd, [1, 2, 3, 4, 5, 6]) 6 &

Python 高阶函数 -- map/reduce

这个内容我是参考廖雪峰的博客,摘抄其中一些内容而来的,附带解决他最后的问题代码. 这是我在C/C++中未曾见过的语法(可能是我学艺未精),理解它确实花了十来二十分钟.它提供了一条google的论文链接:"MapReduce: Simplified Data Processing on Large Clusters",据说是一篇很牛逼的文章.当我理解了这个概念后,觉得确实很方便. 先看map.map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列

[ Python - 9 ] 高阶函数map和reduce连用实例

1. 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456: from functools import reduce def str2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] def str2float(s): if '.' in s: # 将字符串s拆分成list类型 s = s.s

python 内置函数 map filter reduce

map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6,7,8] t = list(map(lambda x:x+10,l)) #遍历 l,l 里的元素全加10 map得到的结果是可迭代对象所以要list print(t) #===>[12, 13, 14, 15, 16, 17, 18] filter(函数名,可遍历迭代的对象) # filter(返回

高阶函数 map,reduce, filter的用法

1. map 用法 1 def fun_C(x): 2 """求平方""" 3 return x ** 2 4 5 6 result = map(fun_C, my_list) 7 print(list(result)) 8 9 my_list1 = ["smith", "edward", "john", "obama", "tom"] 10 11

Python高阶函数_map/reduce/filter函数

本篇将开始介绍python高阶函数map/reduce/filter的用法,更多内容请参考:Python学习指南 map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文"MapReduce: Simplified Data Processing on Large Clusters",你就能大概明白map/reduce的概念. 我们先看map.map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序