Python基础篇 第六节课

作用域

name = "alex"
def foo():
    name  = "KLY"
    def bar():
        print(name)
    bar()
foo()

#输出KLY
name = "alex"
def foo():
    # name  = "KLY"
    def bar():
        print(name)
    bar()
foo()

#输出alex
name = "alex"
def foo():
    name  = "KLY"
    def bar():
        print(name)
    return bar
a = foo()
print(a)
a()

#输出KLY
def test1():
    print("in the test1")
def test():
    print("in the test")
    return test1()

res = test()
print(res)

#输出
in the test
in the test1
None
def foo():
    name = "KLY"
    def bar():
        name = "JZH"
        def tt():
            print(name)
        return tt
    return bar
# bar = foo()
# tt = bar()
# print(tt)
# tt()
foo()()()

#输出JZH

匿名函数

def c(x):
    return x+1
a = c(10)
print(a)

func = lambda x:x+1
print(func(10))

#输出11
name = "LJH"
def change_name(x):
    return name + "_sb"

res = change_name(name)
print(res)

f = lambda x:x+"_sb"
res = f(name)
print(res)

#输出
LJH_sb
LJH_sb
func = lambda x,y,z:x+y+z
print(func(1,2,3))

#输出6
f = lambda x,y,z:(x+1,y+1,z+1)
print(f(1,2,3))

#输出(2, 3, 4)

函数式编程

  把函数当做参数传给另一个函数

def foo(n):
    print(n)
def bar(name):
    print("my name is %s" %name)

foo(bar("LJH"))

def bar():
    print("from bar")
def foo():
    print("from foo")
    return bar
n = foo()
n()

def hanle():
    print("from handle")
    return hanle
n = hanle()
n()

#输出
my name is LJH
None
from foo
from bar
from handle
from handle

尾调用

  最后一步调用函数

def foo(x):
    x += 1
    return x
def a():
    return foo(6)

map函数

num_1 = [2,4,6,8,5,9]
def map_test(array):
    ret=[]
    for i in array:
        ret.append(i**2)
    return ret

ret  = map_test(num_1)
print(ret)

#输出[4, 16, 36, 64, 25, 81]
num_1 = [2,4,6,8,5,9]
def add_one(x):             #lambda x:x+1
    return x+1
def reduce(x):              #lambda x:x-1
    return x-1
def pf (x):                 #lambda x:x**2
    return x**2

def map_test(func,array):
    ret = []
    for i in num_1:
        res = func(i)
        ret.append(res)
    return ret
print(map_test(add_one,num_1))     #print(map_test(lambda x:x+1,num_1))
print(map_test(reduce,num_1))      #print(map_test(lambda x:x-1,num_1))
print(map_test(pf,num_1))          #print(map_test(lambda x:x**2,num_1))

#输出
[3, 5, 7, 9, 6, 10]
[1, 3, 5, 7, 4, 8]
[4, 16, 36, 64, 25, 81]
num_1 = [2,4,6,8,5,9]
def map_test(func,array):
    ret = []
    for i in array:
        res = func(i)
        ret.append(res)
    return ret
print(map_test(lambda x:x+1,num_1))
print(list(map_test(lambda x:x+1,num_1)))

#输出
[3, 5, 7, 9, 6, 10]
[3, 5, 7, 9, 6, 10]
num_1 = [2,4,6,8,5,9]
# def map_test(func,array):
#     ret = []
#     for i in array:
#         res = func(i)
#         ret.append(res)
#     return ret
# print(map_test(lambda x:x+1,num_1))
# print(list(map_test(lambda x:x+1,num_1))
print(list(map(lambda x:x+1,num_1)))

#输出[3, 5, 7, 9, 6, 10]
msg = "konglingyueshishuaige"
print(list(map(lambda x:x.upper(),msg)))

#输出
['K', 'O', 'N', 'G', 'L', 'I', 'N', 'G', 'Y', 'U', 'E', 'S', 'H', 'I', 'S', 'H', 'U', 'A', 'I', 'G', 'E']

filter函数

Moive_people = {"SB_JZJ","SB_HJHJ","SB_JKDH","KLY"}
ret = []
for i in Moive_people:
    if not i .startswith("SB"):
        ret.append(i)
print(ret)

#输出['KLY']
Moive_people = {"SB_JZJ","SB_HJHJ","SB_JKDH","KLY"}

def filter_test(array):
    ret = []
    for i in array:
        if not i .startswith("SB"):
            ret.append(i)
        return ret
res = filter_test(Moive_people)
print(res)

#输出['KLY']
oive_people = {"SB_JZJ_SB","SB_HJHJ_SB","SB_JKDH_SB","KLY"}

def  sb_show(n):                #lambda n:n.endswith("SB")
    return n.endswith("SB")
def filter_test(func,array):
    ret=[]
    for i in array:
        if not func(i):
            ret.append(i)
        return ret

res = filter_test(sb_show,Moive_people)         #res = filter_test(lambda n:n.endswith("SB"),Movie_people)
print(res)                                      #print(res)


#输出['KLY']
Moive_people = {"SB_JZJ_SB","SB_HJHJ_SB","SB_JKDH_SB","KLY"}
# def  sb_show(n):
#     return n.endswith("SB")
# def filter_test(func,array):
#     ret=[]
#     for i in array:
#         if not func(i):
#             ret.append(i)
#         return ret
#
# res = filter_test(sb_show,Moive_people)
# print(res)
print(list(filter(lambda n:not n.endswith("SB"),Moive_people)))
#输出['KLY']

reduce 函数

res = 0
num_1 = [1,3,5,698,5]
for num in num_1:
        res +=num

print(res)


#输出712
a = [1,5,6,486,444]
def reduce_test(array):
    res = 0
    for num in array:
        res +=num
    return res
print(reduce_test(a))

#输出942
a = [1,5,52,486,444]
def reduce_test(func,array):
    res = array.pop(0)
    for num in array:
        res = func(res,num)
    return res
print(reduce_test(lambda x,y:x*y,a))

#输出56103840
a = [1,5,52,486,444]
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
print(reduce_test(lambda x,y:x*y,a,1))


#输出 56103840
from functools import reduce
a = [1,5,52,486,444]
print(reduce(lambda x,y:x+y,a,2))


#输出990

小结:

  map     处理序列中的乜咯元素,得到的结果是一个列表,该列表元素及位置与原来的一样

  filter     遍历序列中的每个元素,判断每个元素得到的布尔值,如果是True则留下

  reduce    处理一个序列,然后把序列进行合并操作

内置函数

# print(abs(-456))                #绝对值
# print(all([1,2,"1"])          #判断有无空值,有则False
#
# name = "你好"
# print(bytes(name,encoding = "utf-8"))
# print(bytes(name,encoding = "utf-8").decode("utf-8"))

# print(bool(""))                 #判断布尔值,0 None,空都为False
# print(bin(12))                   #把十进制转为二进制
# print(hex(45))                      #把十进制转为十六进制
# print(oct(456))                     #把十进制转为八进制
# print(chr(45))                          #转化为一个值
# print(divmod(10,3))                     #取商留余
# dic  = {"name":"alex"}
# dic_str = str(dic)
# dic_str
# eval(dic_str)
# d1 =eval(dic_str)
# print(d1["name"] )                             #把字符串中的数据结构提取出来
# express = "1+2*(3/3-1)-2"
# print(eval(express))                           #把字符串中的表达式进行运算
# print(hash("541515dsds215"))                     # hash特征    长度固定    不能根据hash的值反推出字符串    在一个程序中,只要变量不变,hash值不会改变
# print(isinstance("abc",str))                        #判断前一个变量是否为后一个类型
# print(globals())                                #全局变量
# print(locals())                                   #局部变量
# l = [1,3,100,-120,566]
# print(max(l))                                      #取最大值
# print(min*(l))                                     #取最小值

 

posted @ 2018-09-04 18:37  Lune23333  阅读(116)  评论(0)    收藏  举报