• python学习之路-day4


    1.装饰器

      代码:

    import time
    
    def timer(func):  #timer(test1)
        def deco(*args,**kwargs): # *args是为了装饰不同的函数,因为有的函数是需要传参数,有的不需要
            start_time=time.time()
            res=func(*args,**kwargs)  #run test1()
            stop_time=time.time()
            print("the func run time is %s"  %(stop_time-start_time))
            return res
        return deco  #这里返回deco函数的内存地址,方便后面调用
    
    @timer  #test1=timer(test1)  #执行timer(test1)返回的是deco的内存地址,
                                #将它赋值给test1就可以得到deco函数的值
    def test1():
        time.sleep(3)
        print('in the test1')
    
    test1()
    View Code

    2.内置方法:

    print(all([1,-5,0]))#判断元素不为0或空,只要有一个含0就为假
    print(any([]))  #判断元素是否为空
    print(ascii('开始'))#把一个数据对象变成字符串的对象
    print(bin(5))#将数字转为2进制
    print(bool([0,3]))#判断元素不为0或空,只有有一个以上不为0元素就为真
    print(bytes("abcde",encoding="utf-8"))
    print(chr(100))#返回ascii码的对应表的字符
    print(ord("d"))#返回字符对应的ascii码
    # dict()生成字典
    print(divmod(5,3)) #返回  余数,商
    #eval()字符串变成字典
    print(hex(14))#十进制转为16进制
    print(oct(17))#十进制转为8进制
    print(round(1.5454,3))#选择保留几位小数
    #slice切片
    a={6:2,5:0,2:0,3:2}
    print(sorted(a.items()))#按照指定的方式排序
    print(type(a))#查看对象的数据类型
    
    A=[1,2,3,4,5,6]
    B=['a','b','c','d']
    for i in zip(A,B):#  拉链
        print(i)
    View Code

    3.生成器并行

    import time
    def consumer(name):
        print("%s 准备吃包子啦!" %name)
        while True:
            baozi = yield
            print("包子[%s]来了,被[%s]吃了!"  %(baozi,name))
    # c=consumer("wang")
    # c.__next__()
    
    def producder(name):
        c=consumer('A')
        c2=consumer('B')
        c.__next__()
        c2.__next__()
        print("老子开始准备做包子啦!")
        for i in range (10):
            time.sleep(1)
            print("做了1个包子,分两半!")
            c.send(i)
            c2.send(i)
    producder("wang")
    View Code

     

  • 相关阅读:
    adb shell top
    数据清洗的方法
    Devices Tree加载流程
    Android驱动之设备树简介
    序列模式挖掘综述
    python 实现kmeans聚类
    numpy中sum(axis=0)和axis=1的计算原理
    win7 VMware下安装centos和Ubuntu共存
    python数据标准化
    python 用PIL Matplotlib处理图像的基本操作
  • 原文地址:https://www.cnblogs.com/awanghang/p/13257483.html
Copyright © 2020-2023  润新知