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()已不再是 ...
随机推荐
- 从网络上获取图片,并写入excel文件
package com.weChat.utils; import com.manage.utils.DateUtil;import com.manage.utils.MD5Util;import or ...
- JS调用本地设备
JS 允许通过 navigator.mediaDevices.getUserMedia(options) 直接调用本地的设备,比如麦克风,摄像头等.因为该操作涉及到用户隐私,所以调用的时候会弹框请求权 ...
- mysql数据库的备份
有时会遇到需要重装虚拟机的情况,这时候之前创建好的数据库就需要备份啦,等重新装好虚拟机直接导入就可以正常使用. 数据库备份大体分为两步: 第一步.导出数据库,因为是备份,会将所有的数据库导出,因此需要 ...
- Autolayout Breakpoints
articles archives team Autolayout Breakpoints Auto layout has become a crucial tool for iOS and OS X ...
- c++ complie link error 2019/2001
1:没有将需要的文件添加到项目中,只是考到项目所在的目录了 2:没有添加 类导出 宏
- 数组中array==null和array.length==0的区别
//代码public class Test1 { public static void main(String[] args) { int[] a1 = new int[0]; int[] a2 = ...
- Zookeeper原理、安装、基本使用和API
ZooKeeper ZooKeeper是一种分布式协调服务, 解决应用程序的分布式带来的问题. 1 分布式应用 分布式应用可以在给定时间(同时)在网络中的多个系统上运行,通过协调它们以快速有效的 ...
- Altium Designer 10 快捷键笔记
一.放置.走线类: 1.交互式走线(Track):P T 2.铺铜(Fill):P F 3.大面积铺铜(Polygon):P G 4.自动扇出:元件封装上右键,C F 二.编辑类 1.调整铺铜(Pol ...
- blueprint的使用
第一步:导入蓝图模块: from flask import Blueprint 第二步:创建蓝图对象: #Blueprint必须指定两个参数,admin表示蓝图的名称,__name__表示蓝图所在模块 ...
- 初用jdbc来运行事务
dao层 public Connection getConnection() throws Exception { Class.forName(driver); if (con == null || ...