• Day6: Anonymous and inner functions


    """

    #Anonymous functions

    def cal(x):
    return x+1
    res = cal(10)
    print(res)
    
    func = lambda x : x+1
    print(func(10))
    
    # fun = x:x+'_sb'
    
    func = lambda x,y,z: x+y+z
    printq(func(1,2,3))
    func = lambda x,y,z: (x+1,y+1,z+1)
    print(func(1,2,3))

    #*******the fuctional theory********

    1:face to process
    2:face to fuction
    3:face to object

    # Higher-order function
    #******1:transfer a function to another function as a parameter

    def foo(n):
    # print(n)
    return n
    
    def bar(name):
    print('my name is %s'%name)
    foo(bar('alex'))

    #******2:return a function

    def bar():
    print('alex')
    def foo():
    print('zxver')
    return bar
    a = foo()
    a()

    # *************map finction*********

    a = [65,88,2,5,6,34,12]
    def map_test(func,array):
    a1 = []
    for i in array:
    res = func(i)
    a1.append(res)
    return a1
    print(map_test(lambda x:x+1,a))
    res = map(lambda x:x+1,a)
    print('the inner setting function is :',res)
    print(list(res))
    
    msg = 'I am zxver'
    print(list(map(lambda x:x.upper(),msg)))

    # ************filter****************

    program_people = ['alex','sb_alex','zxver','sb_zxver']
    ret = []
    for p in program_people:
    if not p.startswith('sb'):
    ret.append(p)
    print(ret)

    #>>>>>>>>filter:

    program_people = ['alex','sb_alex','zxver','sb_zxver']
    def filter_test(array):
    ret = []
    for p in array:
    if not p.startswith('sb'):
    # not appropriate for other demand
    ret.append(p)
    return ret
    print(filter_test(program_people))

    #*******refined:>>>>>>>

    program_people = ['alex','sb_alex','zxver','sb_zxver']
    # def sb_show(n):
    # n.endswith('sb')
    # the best version:no the define above
    def filter_test(func,array):
    ret = []
    for i in array:
    if not func(i):
    ret.append(i)
    return ret
    #print(filter_test(sb_show,program_people))
    res = filter_test(lambda x:x.endswith('sb'),program_people)
    print(res)

    """
    # *********reduce function**********

    # from functools import reduce
    num_1 = [34,56,12]
    def reduce_test(func,array,init=None):
    if init is None:
    ret = array.pop(0)
    else:
    ret = init
    for i in array:
    ret = func(ret,i)
    return ret
    print(reduce_test(lambda x,y:x*y,num_1,100))

    # Inner functions

    """

    print(abs(-1))
    print(all(''))
    # a blank is true
    print(all(['a']))
    print(all(['',0]))
    print(any(['',1]))
    name = 'hello'
    print(bytes(name,encoding = 'utf-8'))
    print(bytes(name,encoding = 'utf-8').decode ('utf_8'))
    print(bytes(name,encoding = 'gbk'))
    print(bytes(name,encoding = 'gbk').decode ('gbk'))
    print(bytes(name,encoding = 'ascii'))
    # can't be used for Chinese
    print(chr(46))
    print(dir(dict))
    print(help(all))
    print(divmod(10,3))
    dic = {'name':'alex'}

    # ********************eval

    dic_str = str(dic)
    print(dic_str)
    print(eval(dic_str))
    express = '1-(4*5-6)/2'
    print(eval(express))

    # ***********hash:can be used to judge if the content been changed

    print(hash(name))
    print('>>>before',hash(name))
    name='zx'
    print('>>>after',hash(name))
    print(bin(10))
    print(hex(12))
    print(oct(6))
    a = [1,4,6,9]
    print(max(a))
    print(min(a))

    #*******zip:use for sequence:tuple,list and string

    print(list(zip(('alex',3,[5,6]),(1,2,3))))
    p = {'name':'alex','age':26,'gender':'man'}
    print(list(zip(p.keys(),p.values())))

    # ***** max(min): for iterable,like the for sentence:extract every character to compare and can't be used for different types;every chareacter compared with sequence ,if the fisrt character has been judged ,there's no need to compare the left characters

    dic = {'alex':19,'zxver':26}
    print(max(zip(dic.keys(),dic.values())))
    
    people = [
    {'name':'zxver','age':19},
    {'name':'chen','age':26},
    {'name':'keyu','age':18},
    {'name':'linda','age':56},
    ]
    print(max(people,key=lambda dic:dic['age']))
    print(sorted(people,key=lambda dic:dic['age']))
    
    print(chr(97))
    print(ord('a'))
    print(str({'name':'alex'}))
    dic_str=str({'name':'alex'})
    print(eval(dic_str))
    print(pow(2,3))
    print(pow(2,3,4)) 
    # 2**4%4
    a=[1,2,3,4]
    print(sum(a))
    print(list(reversed(a)))
    print(round(3.4))
    print(set('hello'))
    l = 'hello'
    a1=slice(2,3)
    a2 = slice(1,5,2)
    print(l[a2])
    def test():
    msg ='snhxhdnsjjs'
    print(locals())
    print(vars())
    print(vars(int))
    test()

    """
    # import:can't use for string-->>sys-->>_import_():can be used for string

  • 相关阅读:
    bootstrap实战练习中涉及的知识点(很有用哦!)
    Markdown的最常用标记符号有哪些?
    队列的插入 和出列 阻塞 时间 问题
    ArrayBlockingQueue和LinkedBlockingQueue的使用
    自己总结 :并发队列ConcurrentLinkedQueue、阻塞队列AraayBlockingQueue、阻塞队列LinkedBlockingQueue 区别 和 使用场景总结
    战斗由客户端来做,后端来验证 方式 解决 一些弊端思路
    类 文件 右下角呈现 红色小圆圈,里面有一个J 标记
    苏州儿童医保
    Error: opening registry key 'SoftwareJavaSoftJava Runtime Environment' could not find java.dll
    intellj(idea) 编译项目时在warnings 页签框里 报 “xxx包不存在” 或 “找不到符号” 或 “未结束的字符串字面值” 或 “需要)” 或 “需要;”等错误提示
  • 原文地址:https://www.cnblogs.com/zxver/p/12124262.html
Copyright © 2020-2023  润新知