map、filter、reduce函数
map
#函数需要⼀个参数
m1 = map(lambda x:x*x,[1,2,3])
print(list(m1)) #函数需要两个参数
m2 = map(lambda x,y:x+y,[1,2,3],[4,5,6])
print(list(m2)) def f1(x,y):
return (x,y)
l1 = [0,1,2,3,4,5,6]
l2 = ['Sun','M','T','W','T','F','S']
l3 = map(f1,l1,l2)
print(list(l3))
输出
[1, 4, 9]
[5, 7, 9]
[(0, 'Sun'), (1, 'M'), (2, 'T'), (3, 'W'), (4, 'T'), (5, 'F'), (6, 'S')]
filter
from collections import Iterator
f1 = filter(lambda x:x%2, [1,2,3])
print(f1)
print(list(f1))
print(isinstance(f1, Iterator))
输出
<filter object at 0x00000176DB8075C0>
[1, 3]
True
reduce
from functools import reduce
r1 = reduce(lambda x,y:x+y,[1,2,3,4])
r2 = reduce(lambda x,y:x+y,[1,2,3,4],5)
r3 = reduce(lambda x,y:x+y,['aa','bb','cc'],'dd')
print(r1)
print(r2)
print(r3)
输出
10
15
ddaabbcc
map、filter、reduce函数的更多相关文章
- Python map,filter,reduce函数
# -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...
- python map() filter() reduce()函数的用法以及实例
map() 看一下我的终端咋说: map()的函数用法: map(function, iterable, ...) 看一下具体例子: 注意的是一定要强制转化一下才能输出 也可以写匿名函数: (mark ...
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- 数组的高阶方法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 ...
- Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)
感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. Python内 ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
- python Map()和reduce()函数
Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...
- 高阶函数map(),filter(),reduce()
接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions. map()和filter()是内置函数.在python3中,reduce()已不再是 ...
随机推荐
- Centos7.4 防火墙配置
# service firewalld status; #查看防火墙状态 (disabled 表明 已经禁止开启启动 enable 表示开机自启,inactive 表示防火墙关闭状态 activate ...
- ENVI5.3 影像重采样 和 tiff 保存
输入---之前用envi4.5处理后的2013分类影像---输出重采样的影像 直接在工具栏搜索 resize data---出来对话框, 这里有几种方法----sample line 指的行列号,可以 ...
- HTML5 Canvas爱心时钟代码
这是一款数字时钟动画,数字又多个小爱心组成,又何问起整理,随着时间推进,每一秒钟新数字替换旧数字,旧数字离去使用天女散花动画,花是五颜六色的. 查看效果:http://hovertree.com/te ...
- ES5数组、对象常用方法总结
数组方法: Array.isArray(items);判断判断一个变量是否包含数组数据: forEach(function(value, index, fullArray){ }); every(fu ...
- 利用 SPICE 分析理解心电图前端中的右腿驱动
[导读] 心电图(ECG)学是一门将心脏离子去极(ionic depolarization) 后转换为分析用可测量电信号的科学.模拟电子接口到电极/患者设计中最为常见的难题之一便是优化右腿驱动 ( ...
- Tajima's D
Three ways to assess the nucleotide diversity (heterozygosity).The first is mean pairwise difference ...
- 测试那些事儿—selenium IDE 自动化测试
浏览器 Firefox Setup 35.0.1 安装完成后设置菜单栏 关闭浏览器自动更新 插件配置(必备武器) FireBug Firebug是firefox下的一个扩展,能够调试所有网站语言,如H ...
- 从码云上下载react项目并配置成可运行状态
(第一次写,如有不足之处,欢迎指出) 一.下载项目: 1.首先保证安装了git, 2.然后在本地想要存放项目位置打开git(Git Bash Here),再复制码云中如图所示的地址: 3.在git中输 ...
- jq ajax数据交互
get 与 post 的区别 了解和使用 get和post是HTTP与服务器交互的方式, 说到方式,其实总共有四种:put,delete,post,get. 他们的作用分别是对服务器资源的增,删,改, ...
- Mybatis根据List批量查询List结果
https://blog.csdn.net/qq_36688928/article/details/82783392