我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者。

#定义函数
def my_abs(x):
if x>=0:
return x
else:
return -x
#调用函数
my_abs(-9) #filter/map/reduce/lambda #filter(function,sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple返回。(取决与sequence的类型)
def f(x):
return x%2!=0 and x%5!=0
filter(f,range(1,20)) def f(x):return x!='u'
filter(f,'uhjonu') #map(function, sequence) :对sequence中的item依次执行function(item),执行结果组成一个List返回。
def square(x):return x+x
map(square,range(1,10))
map(square,"abcdef")
#map也支持多个sequence,这就要求function也支持相应数量的参数输入
def plus(x,y):
return x+y
map(plus,range(5),range(5)) #reduce(function, sequence, starting_value):对sequence中的item顺序迭代调用function,如果有starting_value,还可以作为初始值调用
def plus(x,y):
return x+y
reduce(plus,range(1,10))
reduce(plus,range(1,10),20) #lambda的用法
g=lambda x:x*2
g(8)

我把run出来的结果也贴在下面了,可能软件安装时出了一点问题,结果显示不是很好看,但很容易理解。

def my_abs(x):
... if x>=0:
... return x
... else:
... return -x
...
>>> my_abs(-9)
9>>> >>>
>>> def f(x):
... return x%2!=0 and x%5!=0
...
>>> filter(f,range(1,20))
[>>> 1, 3, 7, 9, 11, 13, 17, 19] >>> def f(x):return x!='u'
...
>>> filter(f,'uhjonu')
'>>> hjon' >>> def square(x):return x+x
...
>>> map(square,range(1,10))
[>>> 2, 4, 6, 8, 10, 12, 14, 16, 18]
map(square,"abcdef")
[>>> 'aa', 'bb', 'cc', 'dd', 'ee', 'ff']
def plus(x,y):
... return x+y
...
>>> map(plus,range(5),range(5))
[>>> 0, 2, 4, 6, 8] >>> def plus(x,y):
... return x+y
...
>>> reduce(plus,range(1,10))
4>>> 5
reduce(plus,range(1,10),20)
6>>> 5 >>> g=lambda x:x*2
>>> g(8)
1>> >6

Python2.7学习笔记-定义函数、filter/map/reduce/lambda的更多相关文章

  1. 高阶函数 filter map reduce

    const app=new Vue({ el:'#app', data:{ books:[{ id:1, name:"算法导论", data: '2006-1', price:39 ...

  2. filter,map,reduce,lambda(python3)

    1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...

  3. Python内置函数filter, map, reduce

    filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车. 1. filter函数的功能相当 ...

  4. python关于list的三个内置函数filter(), map(), reduce()

    ''' Python --version :Python 2.7.11 Quote : https://docs.python.org/2/tutorial/datastructures.html#m ...

  5. Python 内置函数&filter()&map()&reduce()&sorted()

    常用内置函数 Python 2.x 返回列表,Python 3.x 返回迭代器 在进行筛选或映射时,输出的结果是一个数组,需要list帮助. 如:print(list(map(lambda x:x+1 ...

  6. Python中特殊函数和表达式 filter,map,reduce,lambda

    1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...

  7. Python中 filter | map | reduce | lambda的用法

      1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...

  8. python: filter, map, reduce, lambda

    filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...

  9. Python学习(五)函数 —— 内置函数 lambda filter map reduce

    Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...

随机推荐

  1. leetcode day6

    [13]Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  2. UVa 231 - Testing the CATCHER

    题目大意:一种拦截导弹能拦截多枚导弹,但是它在每次拦截后高度不会再升高,给出导弹的序列,问最多能拦截多少枚导弹? 最长递减子序列问题. #include <cstdio> #include ...

  3. CSS border三角、圆角图形生成技术简介

    http://www.zhangxinxu.com/wordpress/?p=794 一.前言 利用CSS的border属性可以生成一些图形,例如三角或是圆角.纯粹的CSS2的内容,没有兼容性的问题, ...

  4. Rest Project Performace Pressure Test

    首次调整基线和配置修改 机器配置为 CPU: Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.6GHz 24core 内存: 128G JDK Ver: 1.7.0_80 To ...

  5. CSS中position属性( absolute | relative | static | fixed )详解

    我们先来看看CSS3 Api中对position属性的相关定义: static:无特殊定位,对象遵循正常文档流.top,right,bottom,left等属性不会被应用. relative:对象遵循 ...

  6. iconfont.cn阿里巴巴矢量图下载字体图标实战

    1.阿里巴巴矢量图网址:www.iconfont.cn 2.然后用新浪微博账号登录 3.输入要查找的图标相应的关键字,回车 4.滑过要找的图标,点击购物车,让图标存储到暂存架中 5.点击暂存架,存储为 ...

  7. IIS8中添加WCF支持几种方法小结[图文]

    方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少 ...

  8. double减法不准确的那些事儿

    CREATE TABLE `helei` (   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `num1` double DEFAULT NULL ...

  9. 将SWF文件用作资源打包

    使用Flash开发网页游戏少不了与各种美术资源打交道.对于静态资源的那就是各种图片,对于会动的资源可以考虑直接做成swf.制作成swf的美术资源又可以分为两种:一种是直接将关键帧罗列在主时间轴上,那么 ...

  10. OpenCV教程二 - Mat对象与它各种用法

    学习OpenCV大家都会遇到一个对象叫做Mat,此对象非常神奇,支持各种操作.很多初学者因此被搞得头晕脑胀,它各种用法太多太杂,搞得初学者应接不暇,感觉有心无力.无处下手之感.这里我们首先要正本清源, ...