iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >day16-python之函数式编程匿名
  • 365
分享到

day16-python之函数式编程匿名

函数python 2023-01-31 00:01:59 365人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

1.复习 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 name = 'alex' #name=‘lhf’ 4 def change_name(): 5 name

1.复习

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 name = 'alex' #name=‘lhf’
 4 def change_name():
 5     name='lhf'
 6     # global name
 7     # name = 'lhf'
 8     # print(name)
 9     # name='aaaa' #name='bbb'
10     def foo():
11         # name = 'wu'
12         nonlocal name
13         name='bbbb'
14         print(name)
15     print(name)
16     foo()
17     print(name)
18 
19 
20 change_name()

2.匿名函数

 1 #!/usr/bin/env Python
 2 # -*- coding:utf-8 -*-
 3 # def calc(x):
 4 #     return x+1
 5 
 6 # res=calc(10)
 7 # print(res)
 8 # print(calc)
 9 
10 # print(lambda x:x+1)
11 # func=lambda x:x+1
12 # print(func(10))
13 
14 # name='alex' #name='alex_sb'
15 # def change_name(x):
16 #     return name+'_sb'
17 #
18 # res=change_name(name)
19 # print(res)
20 
21 # func=lambda x:x+'_sb'
22 # res=func(name)
23 # print('匿名函数的运行结果',res)
24 
25 # func=lambda x,y,z:x+y+z
26 # print(func(1,2,3))
27 
28 # name1='alex'
29 # name2='sbalex'
30 # name1='supersbalex'
31 
32 
33 
34 # def test(x,y,z):
35 #     return x+1,y+1  #----->(x+1,y+1)
36 
37 # lambda x,y,z:(x+1,y+1,z+1)

3.作用域

 

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # def test1():
 4 #     print('in the test1')
 5 # def test():
 6 #     print('in the test')
 7 #     return test1
 8 #
 9 # # print(test)
10 # res=test()
11 # # print(res)
12 # print(res()) #test1()
13 
14 #函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系
15 # name = 'alex'
16 # def foo():
17 #     name='linhaifeng'
18 #     def bar():
19 #         # name='wupeiqi'
20 #         print(name)
21 #     return bar
22 # a=foo()
23 # print(a)
24 # a() #bar()

4.函数式编程

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #高阶函数1。函数接收的参数是一个函数名  2#返回值中包含函数
 4 # 把函数当作参数传给另外一个函数
 5 # def foo(n): #n=bar
 6 #     print(n)
 7 # #
 8 # def bar(name):
 9 #     print('my name is %s' %name)
10 # #
11 # # foo(bar)
12 # # foo(bar())
13 # foo(bar('alex'))
14 #
15 #返回值中包含函数
16 # def bar():
17 #     print('from bar')
18 # def foo():
19 #     print('from foo')
20 #     return bar
21 # n=foo()
22 # n()
23 # def hanle():
24 #     print('from handle')
25 #     return hanle
26 # h=hanle()
27 # h()
28 #
29 #
30 #
31 # def test1():
32 #     print('from test1')
33 # def test2():
34 #     print('from handle')
35 #     return test1()

4.map函数

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # num_l=[1,2,10,5,3,7]
 4 # num1_l=[1,2,10,5,3,7]
 5 
 6 # ret=[]
 7 # for i in num_l:
 8 #     ret.append(i**2)
 9 #
10 # print(ret)
11 
12 # def map_test(array):
13 #     ret=[]
14 #     for i in num_l:
15 #         ret.append(i**2)
16 #     return ret
17 #
18 # ret=map_test(num_l)
19 # rett=map_test(num1_l)
20 # print(ret)
21 # print(rett)
22 
23 num_l=[1,2,10,5,3,7]
24 #lambda x:x+1
25 def add_one(x):
26     return x+1
27 
28 #lambda x:x-1
29 def reduce_one(x):
30     return x-1
31 
32 #lambda x:x**2
33 def pf(x):
34     return x**2
35 
36 def map_test(func,array):
37     ret=[]
38     for i in num_l:
39         res=func(i) #add_one(i)
40         ret.append(res)
41     return ret
42 
43 # print(map_test(add_one,num_l))
44 # print(map_test(lambda x:x+1,num_l))
45 
46 # print(map_test(reduce_one,num_l))
47 # print(map_test(lambda x:x-1,num_l))
48 
49 # print(map_test(pf,num_l))
50 # print(map_test(lambda x:x**2,num_l))
51 
52 #终极版本
53 def map_test(func,array): #func=lambda x:x+1    arrary=[1,2,10,5,3,7]
54     ret=[]
55     for i in array:
56         res=func(i) #add_one(i)
57         ret.append(res)
58     return ret
59 
60 # print(map_test(lambda x:x+1,num_l))
61 res=map(lambda x:x+1,num_l)
62 print('内置函数map,处理结果',res)
63 # for i in res:
64 #     print(i)
65 # print(list(res))
66 # print('传的是有名函数',list(map(reduce_one,num_l)))
67 
68 
69 msg='linhaifeng'
70 print(list(map(lambda x:x.upper(),msg)))

5.filter函数

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao']
 4 
 5 
 6 
 7 
 8 # def filter_test(array):
 9 #     ret=[]
10 #     for p in array:
11 #         if not p.startswith('sb'):
12 #                ret.append(p)
13 #     return ret
14 #
15 # res=filter_test(movie_people)
16 # print(res)
17 
18 # movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
19 # def sb_show(n):
20 #     return n.endswith('sb')
21 #
22 # def filter_test(func,array):
23 #     ret=[]
24 #     for p in array:
25 #         if not func(p):
26 #                ret.append(p)
27 #     return ret
28 #
29 # res=filter_test(sb_show,movie_people)
30 # print(res)
31 
32 #终极版本
33 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
34 # def sb_show(n):
35 #     return n.endswith('sb')
36 #--->lambda n:n.endswith('sb')
37 
38 def filter_test(func,array):
39     ret=[]
40     for p in array:
41         if not func(p):
42                ret.append(p)
43     return ret
44 
45 # res=filter_test(lambda n:n.endswith('sb'),movie_people)
46 # print(res)
47 
48 #filter函数
49 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
50 # print(list(filter(lambda n:not n.endswith('sb'),movie_people)))
51 res=filter(lambda n:not n.endswith('sb'),movie_people)
52 print(list(res))
53 
54 
55 print(list(filter(lambda n:not n.endswith('sb'),movie_people)))

6.reduce函数

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 from functools import reduce
 4 
 5 
 6 # num_l=[1,2,3,100]
 7 #
 8 # res=0
 9 # for num in num_l:
10 #     res+=num
11 #
12 # print(res)
13 
14 # num_l=[1,2,3,100]
15 # def reduce_test(array):
16 #     res=0
17 #     for num in array:
18 #         res+=num
19 #     return res
20 #
21 # print(reduce_test(num_l))
22 
23 # num_l=[1,2,3,100]
24 #
25 # def multi(x,y):
26 #     return x*y
27 # lambda x,y:x*y
28 #
29 # def reduce_test(func,array):
30 #     res=array.pop(0)
31 #     for num in array:
32 #         res=func(res,num)
33 #     return res
34 #
35 # print(reduce_test(lambda x,y:x*y,num_l))
36 
37 # num_l=[1,2,3,100]
38 # def reduce_test(func,array,init=None):
39 #     if init is None:
40 #         res=array.pop(0)
41 #     else:
42 #         res=init
43 #     for num in array:
44 #         res=func(res,num)
45 #     return res
46 #
47 # print(reduce_test(lambda x,y:x*y,num_l,100))
48 
49 #reduce函数
50 # from functools import reduce
51 # num_l=[1,2,3,100]
52 # print(reduce(lambda x,y:x+y,num_l,1))
53 # print(reduce(lambda x,y:x+y,num_l))

7.小结

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数及位置与原来一样
 4 # map()
 5 
 6 #filter遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来
 7 
 8 people=[
 9     {'name':'alex','age':1000},
10     {'name':'wupei','age':10000},
11     {'name':'yuanhao','age':9000},
12     {'name':'linhaifeng','age':18},
13 ]
14 # print(list(filter(lambda p:p['age']<=18,people)))
15 # print(list(filter(lambda p:p['age']<=18,people)))
16 
17 #reduce:处理一个序列,然后把序列进行合并操作
18 from functools import reduce
19 print(reduce(lambda x,y:x+y,range(100),100))
20 # print(reduce(lambda x,y:x+y,range(1,101)))

8.内置函数

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # print(abs(-1))
 4 # print(abs(1))
 5 #
 6 # print(all([1,2,'1']))
 7 # print(all([1,2,'1','']))
 8 # print(all(''))
 9 
10 # print(any([0,'']))
11 # print(any([0,'',1]))
12 
13 
14 # print(bin(3))
15 
16 #空,None,0的布尔值为False,其余都为True
17 # print(bool(''))
18 # print(bool(None))
19 # print(bool(0))
20 
21 name='你好'
22 # print(bytes(name,encoding='utf-8'))
23 # print(bytes(name,encoding='utf-8').decode('utf-8'))
24 
25 # print(bytes(name,encoding='gbk'))
26 # print(bytes(name,encoding='gbk').decode('gbk'))
27 #
28 # print(bytes(name,encoding='ascii'))#ascii不能编码中文
29 #
30 # print(chr(46))
31 #
32 # print(dir(dict))
33 #
34 # print(divmod(10,3))
35 
36 # dic={'name':'alex'}
37 # dic_str=str(dic)
38 # print(dic_str)
39 
40 #可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型
41 # print(hash('12sdfdsaf3123123sdfasdfasdfasdfasdfasdfasdfasdfasfasfdasdf'))
42 # print(hash('12sdfdsaf31231asdfasdfsadfsadfasdfasdf23'))
43 #
44 name='alex'
45 # print(hash(name))
46 # print(hash(name))
47 #
48 #
49 # print('--->before',hash(name))
50 # name='sb'
51 # print('=-=>after',hash(name))
52 
53 
54 # print(help(all))
55 #
56 # print(bin(10))#10进制->2进制
57 # print(hex(12))#10进制->16进制
58 # print(oct(12))#10进制->8进制
59 
60 
61 name='哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈粥少陈'
62 # print(globals())
63 # print(__file__)
64 #
65 def test():
66     age='1111111111111111111111111111111111111111111111111111111111111'
67     # print(globals())
68     print(locals())
69 #
70 # test()
71 #
72 l=[1,3,100,-1,2]
73 # print(max(l))
74 # print(min(l))
75 
76 
77 
78 # print(isinstance(1,int))
79 # print(isinstance('abc',str))
80 print(isinstance([],list))
81 # print(isinstance({},dict))
82 print(isinstance({1,2},set))

 

--结束END--

本文标题: day16-python之函数式编程匿名

本文链接: https://www.lsjlt.com/news/182229.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • day16-python之函数式编程匿名
    1.复习 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 name = 'alex' #name=‘lhf’ 4 def change_name(): 5 name...
    99+
    2023-01-31
    函数 python
  • 浅谈Python函数式编程的返回函数与匿名函数
    目录返回函数匿名函数返回函数 所谓返回函数,顾名思义,就是把函数作为返回值。高阶函数除了可以将函数作为参数之外,还可以将函数作为结果进行返回。下面来实现一个可变参数的连乘,求积函数可...
    99+
    2023-05-15
    Python函数 Python函数式 Python返回函数 Python匿名函数
  • Python函数式编程的返回函数与匿名函数怎么定义
    本篇内容介绍了“Python函数式编程的返回函数与匿名函数怎么定义”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!返回函数所谓返回函数,顾名思...
    99+
    2023-07-06
  • python之高阶函数和匿名函数
    map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。 1 def func(x): 2 return x*x 3 4 r = ma...
    99+
    2023-01-30
    函数 高阶 python
  • python教程之生成器和匿名函数
    目录生成器01 什么是生成器?02 通俗的讲解03 生成器到底有什么用?04 生成器的常见用途?匿名函数01 什么是匿名函数?02 通俗的讲解总结生成器 01 什么是生成器? 记住两...
    99+
    2024-04-02
  • Python 3 之 lambda匿名函
    ------- lambda -------------------------------------    除了def语句之外,Python还提供了一种生成函数对象的表达式形式。由于它与LISP语言中的一个工具很相似,所以称为lambd...
    99+
    2023-01-31
    Python lambda
  • python基础之匿名函数详解
    目录1.匿名函数介绍2.语法3.使用场景4.匿名函数和普通函数的对比5.匿名函数的多种形式6.lambda 作为一个参数传递7. lambda函数与python内置函数配合使用8.l...
    99+
    2024-04-02
  • python基础之匿名函数介绍
    目录前言一、创建一个匿名函数:二、创建一个带参数的匿名函数三、求两个数的中的最大的值四、练习题:前言 在定义函数的时候,不想给函数起一个名字。这个时候就可以用lambda来定义一个匿...
    99+
    2024-04-02
  • Python-3 匿名函数
    #1、匿名函数计算a+b的值 func = lambda a,b:a+b result = func(2,3) #传入实参2和3,计算a+b,自动返回a+b的值。与def ...
    99+
    2023-01-31
    函数 Python
  • python基础之引用和匿名函数
    a=1 #1 为对象, def func(x): print('x的地址{}'.format(id(x))) x=2 print('x的地址{}'...
    99+
    2024-04-02
  • Python匿名函数详情
    目录1、匿名函数2、内置函数使用1、匿名函数 在python中,除了一般使用def定义的函数外,还有一种使用lambda定义的匿名函数。这种函数可以用在任何普通函数可以使用的地方,但...
    99+
    2024-04-02
  • Python Lambda表达式与其他编程语言的匿名函数对比
    Python Lambda表达式介绍 Lambda表达式是Python中定义匿名函数的一种简洁方式,它使用关键字lambda来定义,后面跟一个参数列表和一个表达式,表达式可以是任何有效的Python表达式,例如: lambda x: x ...
    99+
    2024-02-23
    Python Lambda表达式 匿名函数 函数式编程 闭包
  • Python函数式编程之闭包
    -------------------------函数式编程之*******闭包------------------------ Note: 一:简介 函数式编程不是程序必须要的,但是对于简化程序有很重要的作用。 Python...
    99+
    2023-01-30
    函数 Python
  • Python基础:lambda 匿名函数
    格式 lambda argument1, argument2,... argumentN : expression square = lambda x: x**2 print(square(2)) 与常规函数区别   匿名函数 l...
    99+
    2023-01-31
    函数 基础 Python
  • python-3_函数_匿名函数_正则_
    L=['a','b','c','d']for (offset,item) in enumerate(L):    print offset,item打印的结果:0 a1 b2 c4 d迭代器:for i in range(100):    ...
    99+
    2023-01-31
    函数 正则 python
  • Python 匿名函数lambda 详情
    目录1.前言2.如何使用 lambda3.总结1.前言 在 Python 中,说到函数,大家都很容易想到用 ​​def​​ 关键字来声明一个函数: def Hello():     ...
    99+
    2024-04-02
  • python匿名函数有哪些
    小编给大家分享一下python匿名函数有哪些,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!01  什么是匿名函数?     在python中,匿名函数,顾名思义,就是没有名...
    99+
    2023-06-14
  • python函数和python匿名函数lambda详解
    目录1. python函数1.1 函数的作用1.2 函数定义1.3 函数调用1.4 函数的参数1.4.1 参数的传递1.4.2 参数类型1.4.2.1 位置参数(必备参数)1.4.2...
    99+
    2024-04-02
  • Python匿名函数/排序函数/过滤函数
    一. lamda匿名函数   为了解决一些简单的需求而设计的一句话函数 # 计算n的n次方 def func(n): return n**n print(func(10)) f = lambda n: n**n print(f(...
    99+
    2023-01-31
    函数 Python
  • Python函数式编程之装饰器
    原则:对修改是封闭的,对扩展是开放的,方法:一般不修改函数或者类,而是扩展函数或者类一:装饰器 允许我们将一个提供核心功能的对象和其他可以改变这个功能的对象’包裹‘在一起, 使用装饰对象的任何对象与装饰前后该对象的交互遵循完全...
    99+
    2023-01-30
    函数 Python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作