对于文件时间的计算(效率问题)

   import functools
import timedef timeit(fun):
        """这是一个装饰器timeit"""
        @functools.wraps(fun) #可以保留被装饰函数的函数名和帮助信息文档
        def wrapper(*args, **kwargs):  # 接收可变参数和关键字参数
            """这是一个wrapper函数"""
            # args:元组  kwargs:字典
            # 在函数执行之前
            start_time = time.time()
            # 执行函数
            res = fun(*args, **kwargs)
            # 函数执行之后
            end_time = time.time()
            print('运行时间为:%.6f' % (end_time - start_time))
            return res
        return wrapper

计算两个程序运行的时间

@timeit
def copy(sourcefile,destfile):
    with open(sourcefile) as f1:
        content = f1.read()
        with open(destfile,'w') as f2:
            f2.write(content)

@timeit
def copy1(sourcefile,destfile):
    with open(sourcefile) as f1,open(destfile,'w')as f2:
        # for line in f1:
        #     f2.write(line)
        f2.write(f1.read())

copy('data.txt','data_1.txt')
copy1('data.txt','data_2.txt')

对于字典里面嵌套字典进行排序

d = {
    '003':{
        'name':'apple1',
        'count':100,
        'price':10
    },
    '002':{
        'name':'apple2',
        'count':200,
        'price':2
    }

}
print(d.items())  将字典中的key与对应的value值组成一个元组
print(sorted(d.items(),key=lambda x:x[1]['count'])) 通过字典的count进行比较
print(sorted(d.items(),key=lambda x:x[1]['price'])) 通过字典的prioce进行比较
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值