python lambda表达式&map/filter/reduce
习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即:
|
1
2
3
4
5
6
7
8
|
# 普通条件语句if 1 == 1: name = 'wupeiqi'else: name = 'alex' # 三元运算name = 'wupeiqi' if 1 == 1 else 'alex' |
对于简单的函数,也存在一种简便的表示方式,即:lambda表达式
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# ###################### 普通函数 ####################### 定义函数(普通方式)def func(arg): return arg + 1 # 执行函数result = func(123) # ###################### lambda ###################### # 定义函数(lambda表达式)my_lambda = lambda arg : arg + 1 # 执行函数result = my_lambda(123) |
lambda存在意义就是对简单函数的简洁表示
内置函数 二
一、map
遍历序列,对序列中每个元素进行操作,最终获取新的序列。

二、filter
对于序列中的元素进行筛选,最终获取符合条件的序列

三、reduce
对于序列内所有元素进行累计操作


print filter(lambda x : x % 2 == 0, range(10))
print map(lambda x, y : x * y, range(1,4), range(1,4))
print reduce(lambda x, y : x + y, range(101))
输出结果:
[0, 2, 4, 6, 8]
[1, 4, 9]
5050
python lambda表达式&map/filter/reduce的更多相关文章
- lambda表达式&map&filter&yield
一.先来看下lambda表达式 1.lambda表达式其实很简单,他是简单的函数的变种,只有三部分组成,之前老师没有讲清楚,今天看书,终于明白了,写个博客记录下 lambda关键字+参数+返回值,参数 ...
- 07 Python中zip(),map(),filter(),reduce()用法
一. zip() zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 ...
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- Python之内建函数Map,Filter和Reduce
Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...
- lambda、map、reduce、filter函数讲解
# coding:utf-8 """ 几个特殊的函数: lambda lambda后面直接跟变量 变量后面是冒号 冒号后面是表达式,表达式计算结果就是本函数的返回值 作用 ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- python lambda表达式简单用法【转】
python lambda表达式简单用法 1.lambda是什么? 看个例子: g = lambda x:x+1 看一下执行的结果: g(1) >>>2 g(2) >>& ...
- 如何在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 ...
随机推荐
- Eigen库笔记整理(二)
Eigen/Geometry 模块提供了各种旋转和平移的表示 旋转矩阵直接使用 Matrix3d 或 Matrix3f Eigen::Matrix3d rotation_matrix = Eigen: ...
- vue-quill-editor富文本焦点问题
vue-quill-editor富文本渲染完成自动获取焦点,问题在于数据请求完成,富文本内容发生变化从而自动获取焦点 mounted() { this.$refs.myQuillEditor.quil ...
- 关于fragment+viewpager的优化
上次写了一个问答项目,用的fragment+viewpager架构,后来发现,划了几次之后,再划回来,会重新加载布局,重新获取数据,这样整个程序和卡,并且占用太多的网络资源. 当时的解决办法是,自己重 ...
- springmvc视图解析
SpringMVC 视图解析的几种方式: 在视图解析的过程中,需要知道逻辑view的名字,model的名字以访问model和view. 使用jsp进行解析,InternalResourceViewRe ...
- cin的返回对象
//有时间回来补坑 //先记上几个有用的博客 https://blog.csdn.net/candj/article/details/4419585https://www.cnblogs.com/gy ...
- TestNG超时测试
用@Test(timeOut = XXX) 指定超时时间,单位是毫秒 package com.janson; import org.testng.annotations.Test; public cl ...
- Spring 使用注解注入 学习(四)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Apache Maven Cookbook(八)学习笔记-Handling Typical Build Requirements
Including and excluding additional resources Using the Maven Help Plugin: mvn help:effective-pom mvn ...
- 【Tomcat】tomcat配置多域名和虚拟路径
当我们用浏览器在访问网页的时候,如访问www.baidu.com,一般都认为会在DNS服务器上找这个域名对应的IP,然后向这个IP发送请求 并响应,其实在DNS服务器解析之前,本机会先在你的系统配置文 ...
- [bzoj1617][Usaco2008 Mar]River Crossing渡河问题_动态规划
River Crossing渡河问题 bzoj-1617 Usaco-2008 Mar 题目大意:题目链接. 注释:略. 想法:zcs0724出考试题的时候并没有发现这题我做过... 先把m求前缀和, ...