day16_函数作用域_匿名函数_函数式编程_map_reduce_filter_(部分)内置函数

# # 1、全局变量与局部变量

# # 2、全局变量与局部变量   nonlocal

# # 3、函数里头调用函数

# # 4、返回函数中的子函数

# # 5、多级函数体的套用

# # 6、函数()()()...()

# # 7、匿名函数lambda

# # 8、编程的方法论:(关于解释,可能不太正确,需要后期补充知识再来修正)
# # # 1、面向过程编程
# # #     功能的实现,是一步一步分步骤实现
# # # 2、函数式
# # #   通过函数的方式实现功能,函数式实现过程中,只允许输入的时候有信息输入,函数体的实现过程中,不允许有
# # #   定义内容等
# # # 3、面向对象编程
# # # ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>
# # # ===>特点:
# # # 面向过程:根据业务逻辑从上到下写垒代码
# # # 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可
# # # 面向对象:对函数进行分类和封装,让开发“更快更好更强...”

# # # 函数式编程:	易读性相对较差
# # # 过程式编程:	易读性相对较高

# # # y = 2*x + 1
# # # 函数式编程( 但这个例子不是非常准确,只用于理解 )

# # # 函数式编程语言:		Hashell clean;	erlang

# # # 函数式编程
# # #
# # # 1.不可变数据
# # #
# # # 2.第一类对象:函数即为变量
# # #
# # # 3.尾调用优化(尾递归)


# # # Python中的函数与变量 - Python少年 - 博客园
# # # https://www.cnblogs.com/echoboy/p/8973266.html


# # 9、高阶函数(含有其下至少一种特性的函数)
# # # # 1、把函数当作参数传给另外一个参数
# # # # 2、返回值中包含函数
# # # ===>通俗讲,就是把某一个函数体,放在,另外一个函数体的“入口”,或者出口的位置
# # # # 把函数当作参数传给另外一个参数    

# # 10、返回值中包含函数


# # 11、返回值中包含函数
# # # # 返回的也可以是函数体的体身内容,注意返回的是“”“”函数的地址“”“”

# # 12、求平方
# # 13、过程式编程

# # 14、对象式编程,函数体实现

# # 15、使用内置函数 map(功能,处理内容)
# # # # 关于map使用的注意事项:
# # # # map返回的是一个迭代器,如果使用for进行输出的话,那么for运行结束后,迭代器的内容就全部输出完毕,
# # # # =====》变为空

# # 16、filter(功能,内容)    
# # filter的第一个参数为真,就保留结果,  因此要过滤掉参数中含有开始为“sb”的内容,应该在lambda中加入not


# # 17、map reduce filter总结
# # # reduce() 函数会对参数序列中元素进行累积。
# # # 函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function
# # # (有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,
# # # 最后得到一个结果。
# # # reduce 函数 实现上述的功能

# # 18、关于reduce的求和实例
# # # # reduce: 处理一个序列,然后把序列进行合并

# # 19、abs() 函数返回数字的绝对值。

# # 20、all
# # # all() 函数用于判断给定的可迭代参数 iterable 中的所有元素是否   都为 TRUE   ,如果是返回 True,否则返回 False。
# # # 元素除了是 0、空、FALSE 外都算 TRUE。

# # 21、any()
# # # # any() 函数用于判断给定的可迭代参数 iterable 是否   全部为 Fals  e,则返回 False,如果有一个为 True,则返回 True。
# # # # 元素除了是 0、空、FALSE ,None 外都算 TRUE。

# # 22、bin
# # # bin() 返回一个整数 int 或者长整数 long int 的二进制表示。

# # 23、bool
# # # # bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False。

# # 24、bytes
# # # 描述c
# # # bytes 函数返回一个新的 bytes 对象,该对象是一个 0 <= x < 256 区间内的整数不可变序列。它是 bytearray 的不可变版本。
# # #
# # # 语法 #
# # # 以下是 bytes 的语法:
# # # class bytes([source[, encoding[, errors]]])
# # # 参数 #
# # # 如果 source 为整数,则返回一个长度为 source 的初始化数组;
# # # 如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列;
# # # 如果 source 为可迭代类型,则元素必须为[0 ,255] 中的整数;
# # # 如果 source 为与 buffer 接口一致的对象,则此对象也可以被用于初始化 bytearray。
# # # 如果没有输入任何参数,默认就是初始化数组为0个元素。


# # 25、encoding  decode 编码与解码

# # 26、chr

# # 27、dir()
# # # dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。
# # # 如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
# # #

# # 28、divmod
# # # python divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。
# # # 在 python 2.3 版本之前不允许处理复数。
# # #
# # # 函数语法
# # # divmod(a, b)
# # #
# # # 参数说明:
# # # a: 数字
# # # b: 数字
# # # 关于应用实例说明,求出的 整数+余数       能够应用于搜索页面的显示。如百度搜索“信息a”,10条,一页3条
# # # 那么,divmod(10 , 3)==>  (3, 1) ,就是说,显示需要3页+1条,  那么结果应该,显示4页    

# # 29、str与字典

# # 30、eval()
# # # =====>   下列的应用,实际上就是去掉字符串两边的 “  ""  ”符号,返回里头的表达式
# # # =====>   也就是把字符串中的数据结构给提取出来
# # # 描述
# # # eval() 函数用来执行一个字符串表达式,并返回表达式的值。
# # #
# # # 语法
# # # 以下是 eval() 方法的语法:
# # #
# # # eval(expression[, globals[, locals]])
# # # 参数
# # # expression -- 表达式。
# # # globals -- 变量作用域,全局命名空间,如果被提供,则必须是一个字典对象。
# # # locals -- 变量作用域,局部命名空间,如果被提供,可以是任何映射对象。
# # # 返回值
# # # 返回表达式计算结果。

# # 31、hash
# # #   可hash的数据类型即不可变数据类型,
# # # 不可hash的数据类型即  可变数据类型    
# # 
# # 32、help()
# # # # 打印帮助

# # 33、bin  oct  hex
# # # # bin     十进制===》 二进制
# # # # oct     十进制===》 8进制
# # # # hex   十进制    ==>十六进制

# # 34、isinstance()
# # # # 描述
# # # # isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
# # # # isinstance() 与 type() 区别:
# # # # type() 不会认为子类是一种父类类型,不考虑继承关系。
# # # # isinstance() 会认为子类是一种父类类型,考虑继承关系。
# # # #
# # # # 如果要判断两个类型是否相同推荐使用 isinstance()。

# # 35、globals
# # # globals() 函数会以字典类型返回当前位置的全部全局变量。
# # # 参数
# # # 无
# # # 返回值
# # # 返回全局变量的字典。    

# # 36、locals
# # # 描述
# # # locals() 函数会以字典类型返回当前位置的全部局部变量。
# # # 对于函数, 方法, lambda 函式, 类, 以及实现了 __call__ 方法的类实例, 它都返回 True。
# # #    

# # 37、min   max
# # # min       求最小值
# # # max       求最大值
# print("分割线".center(80,"*"))
# **************************************分割线***************************************
# **************************************分割线***************************************
# **************************************分割线***************************************

# # 1、全局变量与局部变量
# name = "alex"
# def chang_name():
#     # name = "lhf"
#     # print(name)
#     global name
#     name = "ccc"
#     print(name)
#
#     def foo():
#         name = "foo!!"
#         print(name)
#     foo()
#
# print(name)
# chang_name()
# print(name)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # alex
# # ccc
# # foo!!
# # ccc
# #
# # Process finished with exit code 0




#
# # 2、全局变量与局部变量   nonlocal
# name = "alex"
#
# def chang_name():
#     # name = "lhf"
#     # print(name)
#     # global name
#     name = "ccc"
#     print(name)
#
#     def foo():
#         nonlocal name
#         name = "foo!!"
#         print(name)
#
#     foo()
#     print("after foo: ",name)
#
#
# print("start: ",name)
# chang_name()
# print("end:   ",name)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # start:  alex
# # ccc
# # foo!!
# # after foo:  foo!!
# # end:    alex
# #
# # Process finished with exit code 0


# 02 03
# 02 03
# 02 03
# 02 03

# # 3、函数里头调用函数
# name = "alex"
#
# def foo():
#     name = "linhaifeng"
#     def bar():
#         print(name)
#
#     bar()
#
# foo()
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # linhaifeng
# #
# # Process finished with exit code 0


#
# # 4、返回函数中的子函数
# name = "alex"
#
# def foo():
#     name = "linhaifeng"
#     def bar():
#         print(name)
#         return 0            # 0会被打印出来》  print(a)中实现
#     return bar()            # 带括号,  表示
#     # 返回整个函数体,          bar() 函数  会被运行
#     # return bar              # 不带括号,表示返回整个函数体的内存地址,bar() 函数不会被运行
# # a = foo                   #这个返回的foo的函数内存地址
# a = foo()
# print(a)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # linhaifeng
# # 0
# #
# # Process finished with exit code 0


# # 5、多级函数体的套用
# # 6、函数()()()...()

# name = "alex"
#
# def foo():
#     name = "linhaifeng"
#     def bar():
#         name = "bar"
#         def tt():
#             print(name)
#         return tt
#     return bar              # 带括号,  表示返回整个函数体,          bar() 函数  会被运行
#     # return bar            # 不带括号,表示返回整个函数体的内存地址,bar() 函数不会被运行
#
#
# bar = foo()
# tt = bar()
# print("print(\"tt:    \" ,tt)".center(60,"*"))
# print("tt:    " ,tt)             # tt的地址
#
# print("print(\"tt():    \" ,tt())".center(60,"*"))
# print("tt():  ",tt())             # 对tt进行运行,并且打印tt的返回值,默认None
#
# print("tt()".center(60,"*"))
# tt()                              # 对tt进行运行
#
# print("另外一种运行 tt的方式".center(60,"-"))
# # 另外一种运行 tt的方式
# foo()()()
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # ********************print("tt:    " ,tt)********************
# # tt:     <function foo.<locals>.bar.<locals>.tt at 0x00000000039EE7B8>
# # ******************print("tt():    " ,tt())******************
# # bar
# # tt():   None
# # ****************************tt()****************************
# # bar
# # ------------------------另外一种运行 tt的方式------------------------
# # bar
# #
# # Process finished with exit code 0



# #
# # 04 05
# # 04 05
# # 04 05
# # 04 05
#
# # 7、匿名函数
# # lambda x: x+1
# # 等同于下面的函数
# def calc(x):
#     return x+1
# res = calc(10)
# print("res:",res)
# print("calc:",calc)             # 直接打印函数 calc的地址
#
# print("lambda x:x+1     :",lambda x:x+1)
# func = lambda x:x+1
# print("func(10):    ",func(10))      # 等同于(lambda x:x+1)(10)
#
# # func1(10) = lambda x:x+1           #=======> 没有这种用法 ,报错
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # res: 11
# # calc: <function calc at 0x00000000003C1EA0>
# # lambda x:x+1     : <function <lambda> at 0x0000000002974620>
# # func(10):     11
# #
# # Process finished with exit code 0



# # 匿名函数演示2
# # 一般函数体的使用
# name = "alex"
# def change_name(x):
#     return x + "--->sb"
#
# res = change_name(name)
# print(res)
#
# # 匿名函数
# func = lambda x: x + "--->sb"
# ni = func("who is ")
# print(func("YouAre"))
# print(ni)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # alex--->sb
# # YouAre--->sb
# # who is --->sb
# #
# # Process finished with exit code 0



# 匿名函数 多组数据
# 一般实现
# def test(x,y,z):
#     return x+1,y+"y",z+"+_+?"
#
# print(test(1,"2","3"))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # (2, '2y', '3+_+?')
# #
# # Process finished with exit code 0




# # 匿名实现
#
# func = lambda  x,y,z: (x+1,y+"y",z+"+_+?")
# print(func(1,"2","3"))
#
# D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# (2, '2y', '3+_+?')
#
# Process finished with exit code 0







# 06
# 06
# 06
# # 8、编程的方法论:(关于解释,可能不太正确,需要后期补充知识再来修正)
# 1、面向过程编程
#     功能的实现,是一步一步分步骤实现
# 2、函数式
#   通过函数的方式实现功能,函数式实现过程中,只允许输入的时候有信息输入,函数体的实现过程中,不允许有
#   定义内容等
# 3、面向对象编程
# ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>  ===>
# ===>特点:
# 面向过程:根据业务逻辑从上到下写垒代码
# 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可
# 面向对象:对函数进行分类和封装,让开发“更快更好更强...”

# 函数式编程:	易读性相对较差
# 过程式编程:	易读性相对较高

# y = 2*x + 1
# 函数式编程( 但这个例子不是非常准确,只用于理解 )

# 函数式编程语言:		Hashell clean;	erlang

# 函数式编程
#
# 1.不可变数据
#
# 2.第一类对象:函数即为变量
#
# 3.尾调用优化(尾递归)


# Python中的函数与变量 - Python少年 - 博客园
# https://www.cnblogs.com/echoboy/p/8973266.html
#
# def cal(x) :
#     return 2*x + 1
#
# # 过程式编程
# def cal(x) :
#     res =2*x
#     res +=1
#     return res
#

# ***************************函数式编程****************************
# ***************************函数式编程****************************
# ***************************函数式编程****************************
# # 9、高阶函数(含有其下至少一种特性的函数)
# # 1、把函数当作参数传给另外一个参数
# # 2、返回值中包含函数
# ===>通俗讲,就是把某一个函数体,放在,另外一个函数体的“入口”,或者出口的位置
# # 把函数当作参数传给另外一个参数
# def foo(n) :    # n = bar
#     print(n)    # bar 的返回值是  None
#
# def bar(name):
#     print("my name is %s" %name)
#
# print("foo(bar): ")
# foo(bar)
#
# print("foo(bar() ): ")
# foo(bar("bar here"))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # foo(bar):
# # <function bar at 0x0000000002974620>
# # foo(bar() ):
# # my name is bar here
# # None
# #
# # Process finished with exit code 0


# # 10、返回值中包含函数
# def bar():
#     print("from bar")
# def foo():
#     print("from foo")
#     return bar
#
# n = foo()
# n()
# print("n:   ",n )
# print("n(): ",n())
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # from foo
# # from bar
# # n:    <function bar at 0x00000000004D1EA0>
# # from bar
# # n():  None
# #
# # Process finished with exit code 0





# # 11、返回值中包含函数
# # 返回的也可以是函数体的体身内容,注意返回的是“”“”函数的地址“”“”
#
#
# def handle():
#     print("from handle")
#     return handle
#
# h = handle()
# h()
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # from handle
# # from handle
# #
# # Process finished with exit code 0




# 08
# 08
# 08
# print("day16_08  map函数".center(80,"*"))
# ********************************day16_08  map函数*********************************
# ********************************day16_08  map函数*********************************
# ********************************day16_08  map函数*********************************

# # 12、求平方
# # 13、过程式编程
# num_1 = [1,2,3,5,7,10]
# # num_2 = [1,2,3,5,7,10]
#
# ret = []
# for i in num_1:
#     ret.append(i**2)
# print(ret)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # [1, 4, 9, 25, 49, 100]
# #
# # Process finished with exit code 0


# # 14、对象式编程,函数体实现
# num_1 = [1,2,3,5,7,10]
#
# def map_test(array):
#     ret = []
#     for i in num_1:
#         ret.append(i**2)
#     return ret
#
# print( map_test( num_1))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # [1, 4, 9, 25, 49, 100]
# #
# # Process finished with exit code 0



# num_1 = [1,2,3,5,7,10]
#
# def reduce_one(x):
#     return x-1
# def add_one(x):
#     return x+1
# def square_itself(x):
#     return x**2
# def map_test(func,array):
#     #func 实现方法程序的地址
#     #array 传入数组或者列表
#     ret = []
#     for i in array:
#         # 实现某功能的方法
#         # ret.append(i**2)      # 求平方方法
#         res = func(i)           # 写成一个步骤就是 ret.append( func(i) )
#         ret.append( res )       # 写成一个步骤就是 ret.append( func(i) )
#     return ret
#
# print( "map_test( square_itself,num_1):")
# print( map_test( square_itself,num_1) )

# print( "map_test( add_one,num_1) :")
# print( map_test( add_one,num_1) )

# print( "map_test( reduce_one,num_1):")
# print( map_test( reduce_one,num_1) )
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # map_test( square_itself,num_1):
# # [1, 4, 9, 25, 49, 100]
# # map_test( add_one,num_1) :
# # [2, 3, 4, 6, 8, 11]
# # map_test( reduce_one,num_1):
# # [0, 1, 2, 4, 6, 9]
# #
# # Process finished with exit code 0





# # 上面函数的另外一种表达方式
# # 使用匿名函数
# num_1 = [1,2,3,5,7,10]
# def map_test(func,array):
#     #func 实现方法程序的地址
#     #array 传入列表
#     ret = []
#     for i in array:
#         # 实现某功能的方法
#         # ret.append(i**2)      # 求平方方法
#         res = func(i)           # 写成一个步骤就是 ret.append( func(i) )
#         ret.append( res )       # 写成一个步骤就是 ret.append( func(i) )
#     return ret
#
# print( "map_test( lambda x :x**2,num_1):")
# print( map_test( lambda x :x**2,num_1) )
#
# print( "map_test( lambda x :x+1,num_1) :")
# print( map_test( lambda x :x+1,num_1) )
#
# print( "map_test( lambda x :x-1,num_1):")
# print( map_test( lambda x :x-1,num_1) )
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # map_test( lambda x :x**2,num_1):
# # [1, 4, 9, 25, 49, 100]
# # map_test( lambda x :x+1,num_1) :
# # [2, 3, 4, 6, 8, 11]
# # map_test( lambda x :x-1,num_1):
# # [0, 1, 2, 4, 6, 9]
# #
# # Process finished with exit code 0

# # 15、map
# # map(功能,处理内容)
# # 另外一种表达方式
# # 使用内置函数 map(功能,处理内容)
# num_1 = [1,2,3,5,7,10]
#
# res1 = map(lambda x: x ** 2, num_1)         # 存储的是map返回的迭代器的地址
# print( "map(lambda x: x ** 2, num_1):")
# print( list(res1) )
#
# res2 = map( lambda x :x+1,num_1)            # 存储的是map返回的迭代器的地址
# print( "map( lambda x :x+1,num_1) :")
# print( list(res2 ) )
#
# res3 = map( lambda x :x-1,num_1)            # 存储的是map返回的迭代器的地址
# print( "map( lambda x :x-1,num_1):")
# print( list(res3 ) )
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # map_test( lambda x :x**2,num_1):
# # [1, 4, 9, 25, 49, 100]
# # map_test( lambda x :x+1,num_1) :
# # [2, 3, 4, 6, 8, 11]
# # map_test( lambda x :x-1,num_1):
# # [0, 1, 2, 4, 6, 9]
# #
# # Process finished with exit code 0





# # 关于map使用的注意事项:
# # map返回的是一个迭代器,如果使用for进行输出的话,那么for运行结束后,迭代器的内容就全部输出完毕,
# # =====》变为空
# # 如上面的列子
#
# num_1 = [1,2,3,5,7,10]
#
# res1 = map(lambda x: x ** 2, num_1)         # 存储的是map返回的迭代器的地址
# for i in res1:
#     print(i)
# print( "map(lambda x: x ** 2, num_1):")
# print( list(res1) )                         # 迭代器的内容被取完,为空
# #
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 1
# # 4
# # 9
# # 25
# # 49
# # 100
# # map(lambda x: x ** 2, num_1):
# # []
# #
# # Process finished with exit code 0


# 09
# 09
# 09
# # 16、filter(功能,内容)

# 过滤掉 movie_people中     开始含有sb 的人
# # 基础形式实现
# movie_people = ["sb_alex","sb_wupeiqi","linhaifeng","sb_yuanhao"]
#
# ret =[]
# for p in movie_people:
#     if not p.startswith("sb"):
#         ret.append(p)
# print(ret)
#
#


# # 函数功能实现
# movie_people = ["sb_alex","sb_wupeiqi","linhaifeng","sb_yuanhao"]
# def filter_test(array):
#     ret =[]
#     for p in array:
#         if not p.startswith("sb"):
#             ret.append(p)
#     return ret
#
# res = filter_test(movie_people)
# print( res)
# #
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # ['linhaifeng']
# #
# # Process finished with exit code 0



# # 函数功能实现
# movie_people = ["sb_alex","sb_wupeiqi","linhaifeng","sb_yuanhao"]
# def filter_test(func,array):
#     ret =[]
#     for p in array:
#         # if not p.startswith("sb"):
#         if not func(p):
#             ret.append(p)
#     return ret
#
# res = filter_test(lambda x: x.startswith("sb"),movie_people)
# print( res)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # ['linhaifeng']
# #
# # Process finished with exit code 0



# # 17、filter(功能,内容)
# # filter实现
# # filter的第一个参数为真,就保留结果
# movie_people = ["sb_alex","sb_wupeiqi","linhaifeng","sb_yuanhao"]
#
# res = filter(lambda x: x.startswith("sb"),movie_people)   # 返回结果的地址
# print( list(res) )
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # ['sb_alex', 'sb_wupeiqi', 'sb_yuanhao']
# #
# # Process finished with exit code 0



# # filter实现
# # filter的第一个参数为真,就保留结果,  因此要过滤掉参数中含有开始为“sb”的内容,应该在lambda中加入not
# #
# movie_people = ["sb_alex","sb_wupeiqi","linhaifeng","sb_yuanhao"]
#
# res = filter(lambda x: not x.startswith("sb"),movie_people)   # 返回结果的地址
# print( list(res) )
# #
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # ['linhaifeng']
# #
# # Process finished with exit code 0





# 10
# 10
# 10
# # 基本功能实现  num_l中的所有的数据相加
# num_1 = [1,2,3,100]
# res =0
# for num in num_1:
#     res +=num
#
# print(res)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 106
# #
# # Process finished with exit code 0

#
# # # 函数功能实现  num_l中的所有的数据相加
# num_1 = [1,2,3,100]
# def reduce_test(array) :
#     res =0
#     for num in array:
#         res +=num
#     return res
# res = reduce_test(num_1)
# print(res)


# # 函数功能实现  num_l中的所有的数据  相加
# num_1 = [1,2,3,100]
# def reduce_test(func, array, init = None) :
#     if init is None:
#         res = array.pop(0)
#     else:
#         res = init
#
#     for num in array:
#         res =func(res,num)
#     return res
#
# res = reduce_test( lambda x,y:  x+y, num_1)
# print(res)
#
# res = reduce_test( lambda x,y:  x*y, num_1,10)
# print(res)
# #
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 106
# # 6000
# #
# # Process finished with exit code 0


# # 函数功能实现  num_l中的所有的数据相乘
# num_1 = [1,2,3,100]
# def reduce_test(func, array, init = None) :
#     if init is None:
#         res = array.pop(0)
#     else:
#         res = init
#
#     for num in array:
#         res =func(res,num)
#     return res
#
# res = reduce_test( lambda x,y:  x*y, num_1)
# print(res)
#
# res = reduce_test( lambda x,y:  x*y, num_1,10)
# print(res)

# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 600
# # 6000
# #
# # Process finished with exit code 0



# # 17、map reduce filter总结

# 11
# 11
# 11、map reduce filter总结
# reduce() 函数会对参数序列中元素进行累积。
# 函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function
# (有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,
# 最后得到一个结果。
# reduce 函数 实现上述的功能
# from functools import reduce
# num_1 = [1,2,3,100]
# print(reduce( lambda x,y:  x*y, num_1,10))        # 10是放大系数

# D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# 6000
#
# Process finished with exit code 0




#
# 处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数及位置与原来一样
# map()
# filter遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来

# people = [
#     {"name": "alex",        "age": 10000},
#     {"name": "wupeiqi",     "age": 1000},
#     {"name": "yuanhao",     "age": 100},
#     {"name": "linhaifeng",  "age": 18},
#
# ]
#
# res = (filter( lambda p: p["age"]<= 18,people))
# # print(filter( lambda p: p["age"]<= 18,people))
# print(list(res))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # [{'name': 'linhaifeng', 'age': 18}]
# #
# # Process finished with exit code 0



# # 18、关于reduce的求和实例
# # reduce: 处理一个序列,然后把序列进行合并
#
# from functools import reduce
# print(reduce(lambda x,y: x+y,range(5),100))   # 100为初始值,实现对闭区间范围【0,4】的所有整数求和: 100+0+1+2+3+4
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 110
# #
# # Process finished with exit code 0





# 12
# 12
# 12
# 12、1  abs
# # 19、abs() 函数返回数字的绝对值。
# print(abs(-1))
# print(abs(1))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 1
# # 1
# #
# # Process finished with exit code 0



# # 20、all
# all() 函数用于判断给定的可迭代参数 iterable 中的所有元素是否   都为 TRUE   ,如果是返回 True,否则返回 False。
# 元素除了是 0、空、FALSE 外都算 TRUE。
# print(all([1,2,"11"]))
# print(all([1,2,"11",""]))
# print(all([1,2,"11",()]))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # True
# # False
# # False
# #
# # Process finished with exit code 0


#
# # 21、 any()
# # any() 函数用于判断给定的可迭代参数 iterable 是否   全部为 Fals  e,则返回 False,如果有一个为 True,则返回 True。
# # 元素除了是 0、空、FALSE ,None 外都算 TRUE。
# print(any([1,2,"11"]))
# print(any([1,2,"11",""]))
# print(any([1,2,"11",()]))
# print(any([0,"",False,None]))



# # 22、bin
# bin() 返回一个整数 int 或者长整数 long int 的二进制表示。
# print(bin(5))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 0b101
# #
# # Process finished with exit code 0


# # 23、bool
# # bool() 函数用于将给定参数转换为布尔类型,如果没有参数,返回 False。
# print("1:               ",bool(1) )
# print("111:             ",bool("111") )
# print("[1,2]            ",bool([1,2]) )
# print("["111","aaa"]    ",bool(["111","aaa"]) )
# print("{"111" :,"aaa"}  ",bool({"111" :,"aaa"}) )
#
# print("[] :              ",bool([]) )
# print("None              ",bool(None) )
# print("{}                q`",bool({}) )


# # 24、bytes
# # 25、encoding  decode 编码与解码

# 描述
# bytes 函数返回一个新的 bytes 对象,该对象是一个 0 <= x < 256 区间内的整数不可变序列。它是 bytearray 的不可变版本。
#
# 语法
# 以下是 bytes 的语法:
# class bytes([source[, encoding[, errors]]])
# 参数
# 如果 source 为整数,则返回一个长度为 source 的初始化数组;
# 如果 source 为字符串,则按照指定的 encoding 将字符串转换为字节序列;
# 如果 source 为可迭代类型,则元素必须为[0 ,255] 中的整数;
# 如果 source 为与 buffer 接口一致的对象,则此对象也可以被用于初始化 bytearray。
# 如果没有输入任何参数,默认就是初始化数组为0个元素。
#
#
# name = "你好"
# print(bytes( name,encoding="utf-8"))            # 输出的是16进制,1个汉字用3个字节比较
# print(bytes( name,encoding="utf-8").decode("utf-8"))
# print(bytes( name,encoding="utf-8").decode())   # 不写的话,python3中默认utf-8
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # b'\xe4\xbd\xa0\xe5\xa5\xbd'
# # 你好
# # 你好
# #
# # Process finished with exit code 0




# name = "你好"
# print(bytes( name,encoding="gbk"))            # 输出的是16进制,1个汉字用2个字节比较
# print(bytes( name,encoding="gbk").decode("gbk"))
# print(bytes( name,encoding="gbk").decode())   # 默认是utf-8 解码,结果出现解码错误
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # b'\xc4\xe3\xba\xc3'
# # 你好
# # Traceback (most recent call last):
# #   File "D:/C_cache/py/python_practice/day16_NiMingHanShu.py", line 864, in <module>
# #     print(bytes( name,encoding="gbk").decode())
# # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte
# #
# # Process finished with exit code 1



# # 26、chr
# chr() 用一个范围在 range(256)内的(就是0~255)整数作参数,返回一个对应的字符。
# print(chr(100) )
# print(chr(45) )
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # d
# # -
# #
# # Process finished with exit code 0




# # 27、dir()
# dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。
# 如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
#
# print(dir(dict))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
# #
# # Process finished with exit code 0






# # 28、divmod
# python divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。
# 在 python 2.3 版本之前不允许处理复数。
#
# 函数语法
# divmod(a, b)
#
# 参数说明:
# a: 数字
# b: 数字
# 关于应用实例说明,求出的 整数+余数       能够应用于搜索页面的显示。如百度搜索“信息a”,10条,一页3条
# 那么,divmod(10 , 3)==>  (3, 1) ,就是说,显示需要3页+1条,  那么结果应该,显示4页
# print(divmod(10 , 3))
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # (3, 1)
# #
# # Process finished with exit code 0





# # 29、str与字典
# dic = {"name": "alex"}
# dic_str = str(dic)
# dic_str1 = "{'name': 'alex'}"           # dic_str1 = "{"name": "alex"}" 这样子会报错
# print("dic:    ",dic    ,type(dic))
# print("dic_str:",dic_str,type(dic_str))
# print("dic_str1:",dic_str1,type(dic_str1))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # dic:     {'name': 'alex'} <class 'dict'>
# # dic_str: {'name': 'alex'} <class 'str'>
# # dic_str1: {'name': 'alex'} <class 'str'>
# #
# # Process finished with exit code 0



# # 30、eval()
# =====>   下列的应用,实际上就是去掉字符串两边的 “  ""  ”符号,返回里头的表达式
# =====>   也就是把字符串中的数据结构给提取出来
# 描述
# eval() 函数用来执行一个字符串表达式,并返回表达式的值。
#
# 语法
# 以下是 eval() 方法的语法:
#
# eval(expression[, globals[, locals]])
# 参数
# expression -- 表达式。
# globals -- 变量作用域,全局命名空间,如果被提供,则必须是一个字典对象。
# locals -- 变量作用域,局部命名空间,如果被提供,可以是任何映射对象。
# 返回值
# 返回表达式计算结果。
#

# dic = {"name": "alex"}
# dic_str = str(dic)
# dic_str1 = "{'name': 'alex'}"           # dic_str1 = "{"name": "alex"}" 这样子会报错
# print("dic:    ",dic    ,type(dic))
# print("dic_str:",dic_str,type(dic_str))
# print("dic_str1:",dic_str1,type(dic_str1))
#
# d = eval(dic_str)
# d1 = eval(dic_str1)
# print(d,type(d))
# print(d1,type(d1))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # dic:     {'name': 'alex'} <class 'dict'>
# # dic_str: {'name': 'alex'} <class 'str'>
# # dic_str1: {'name': 'alex'} <class 'str'>
# # {'name': 'alex'} <class 'dict'>
# # {'name': 'alex'} <class 'dict'>
# #
# # Process finished with exit code 0

# eval()的应用
# 例如计算一个等式,输入的是一个字符串,我们需要将里头的结果计算出来
# express = "1+2*(3/3-1)-2"
# print(express)
# print(eval(express) )
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 1+2*(3/3-1)-2
# # -1.0
# #
# # Process finished with exit code 0




# # 31、hash
#   可hash的数据类型即不可变数据类型,
# 不可hash的数据类型即  可变数据类型

# print(hash("132134649745467897975safsafsafasdfsfdsdfasadf"))
# print(hash("1"))
# print(hash("a"))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # -7424390623122969874
# # -22698245628170108
# # -4943392740421642797
# #
# # Process finished with exit code 0


# # hash 前后的计算编码值保持不变
# name = "alex"
# print(hash(name))
# print(hash(name))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 8721249888825822418
# # 8721249888825822418
# #
# # Process finished with exit code 0


# # hash 编码的内容一旦修改,hash的计算的结果就会改变
# name = "alex"
# print("====>before",hash(name))
# name = "--_--"
# print("====>after ",hash(name))
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # ====>before 7905044469795841811
# # ====>after  -4425303464219513957
# #
# # Process finished with exit code 0


# # 32、help()
# # 打印帮助
# print(help(all))
#
# # D:\Anaconda3\python.exe D: / C_cache / py / python_practice / day16_NiMingHanShu.py
# # Help on built - in function
# # all in module
# # builtins:
# # all(iterable, /)
# # Return True if bool(x) is True for all values x in the iterable.
# #
# # If the iterable is empty,return True.
# # None
# # Process  finished  with exit code 0





# # 33、bin  oct  hex
# # bin     十进制===》 二进制
# # oct     十进制===》 8进制
# # hex   十进制    ==>十六进制
# print(bin(17))
# print(oct(17))
# print(hex(17))
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 0b10001
# # 0o21
# # 0x11
# #
# # Process finished with exit code 0



# # 34、isinstance()
# # 描述
# # isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。
# # isinstance() 与 type() 区别:
# # type() 不会认为子类是一种父类类型,不考虑继承关系。
# # isinstance() 会认为子类是一种父类类型,考虑继承关系。
# #
# # 如果要判断两个类型是否相同推荐使用 isinstance()。
#
# a = 111
# print("(a,int)      ",isinstance(a,int))
# print("(a,str)      ",isinstance(a,str))
# print("(a,list)     ",isinstance(a,list))
# print("(a,tuple)    ",isinstance(a,tuple))
# print("(a,set)      ",isinstance(a,set))
# print("(a,dict)     ",isinstance(a,dict))
#
# print("(\"111\",int))          ",isinstance("111",int))
# print("(a,(str,int,tuple)   ",isinstance(a,(str,int,tuple)))      # a是括号中,即元组内容的子类
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # (a,int)       True
# # (a,str)       False
# # (a,list)      False
# # (a,tuple)     False
# # (a,set)       False
# # (a,dict)      False
# # ("111",int))           False
# # (a,(str,int,tuple)    True
# #
# # Process finished with exit code 0


# # 35、globals
# globals() 函数会以字典类型返回当前位置的全部全局变量。
# 参数
# 无
# 返回值
# 返回全局变量的字典。

# name = "===================>2333二哈哈哈哈哈哈哈哈"
# print(globals())            # 打印出来的最右边有name的信息,因为其是全局变量
# print( __file__ )           # 两个下划线
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000000000059D0F0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/C_cache/py/python_practice/day16_NiMingHanShu.py', '__cached__': None, 'name': '===================>2333二哈哈哈哈哈哈哈哈'}
# # D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# #
# # Process finished with exit code 0





# # 36、locals
# 描述
# locals() 函数会以字典类型返回当前位置的全部局部变量。
# 对于函数, 方法, lambda 函式, 类, 以及实现了 __call__ 方法的类实例, 它都返回 True。
#
# name = "????"
# def P():
#     name = "===================>2333二哈哈哈哈哈哈哈哈"
#     print(name)
#     print("locals:==>",locals())            # 打印出来的最右边有name的信息,因为其是全局变量
#
#
# P()
# print("name:  ",name)
#
# print("globals==>",globals())            # 打印出来的最右边有name的信息,因为其是全局变量
# print("locals:==>",locals())             # 打印出来的最右边有name的信息,因为其是全局变量
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # ===================>2333二哈哈哈哈哈哈哈哈
# # locals:==> {'name': '===================>2333二哈哈哈哈哈哈哈哈'}
# # name:   ????
# # globals==> {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000000000214D0F0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/C_cache/py/python_practice/day16_NiMingHanShu.py', '__cached__': None, 'name': '????', 'P': <function P at 0x00000000020A1EA0>}
# # locals:==> {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000000000214D0F0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/C_cache/py/python_practice/day16_NiMingHanShu.py', '__cached__': None, 'name': '????', 'P': <function P at 0x00000000020A1EA0>}
# #
# # Process finished with exit code 0


# # 37、min   max
# min       求最小值
# max       求最大值
# l = [1,3,100,-1,2]
# print(max(l))
# print(min(l))
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/python_practice/day16_NiMingHanShu.py
# # 100
# # -1
# #
# # Process finished with exit code 0


猜你喜欢

转载自blog.csdn.net/xu380393916/article/details/81239316