• day16作业


    声明式编程:

    1、将names = ['egon','alex_sb','wupeiqi','yuanhu']中的名字全部大写

    names = ['egon','alex_sb','wupeiqi','yuanhu']
    names = map(lambda x:x.upper(),names)
    print(list(names))
    
    #['EGON', 'ALEX_SB', 'WUPEIQI', 'YUANHU']

    2、将names = ['egon','alex_sb','wupeiqi','yuanhu']中的名字全部大写

    names = ['egon','alex_sb','wupeiqi','yuanhu']
    res = filter(lambda name:not name.endswith('sb'),names)
    print(list(res))
    
    #['egon', 'wupeiqi', 'yuanhu']

    3、求文件a.txt中最长的行的长度,(长度以字符个数计算,需要使用max函数)

    f = open(r'a.txt','rt',encoding='utf-8')
    res = len(max(f,key=lambda x:len(x)))
    print(res)
    f.close()
    #30

    4、求a.txt文件中字符的个数,思考在第一次后的n次求和中sum求和的结果都为0,(需要使用sum函数)

    f = open(r'a.txt','rt',encoding='utf-8')
    res = sum(map(lambda x:len(x),f))
    print(res)
    #90

    5、思考题

    with open(r'a.txt','r',encoding='utf-8') as f:
        g = (len(line) for line in f)
    print(sum(g))
    #为何出错

    求sum(g)的时候,文件已经关闭,没有办法是文件迭代生成想要的内容,可以改为:

    f= open(r'a.txt','r',encoding='utf-8')
    g = (len(line) for line in f)
    print(sum(g))
    f.close()

    6、文件shopping.txt内容如下:

    mac,20000,3
    lenovo,3000,10
    tesla,1000000,10
    chicken,200,1

    求总价:

    f = open(r'shopping.txt','r',encoding='utf-8')
    res = map(lambda x:x.strip('
    ').split(','),f)
    total = sum(map(lambda x:float(x[1])*float(x[2]),res))
    print(total)
    f.close()
    #10090200.0
    打印商品信息 格式为:
    [{'name':'xxx','price':'xxxx','count':'xx'}]
    f = open(r'shopping.txt','r',encoding='utf-8')
    # [{'name':'xxx','price':'xxxx','count':'xx'}]
    res = map(lambda x:x.strip('
    ').split(','),f)
    msg = map(lambda x:{'name':x[0],'price':x[1],'count':x[2]},res)
    msg_list=[i for i in msg]
    print(msg_list)
    #[{'name': 'mac', 'price': '20000', 'count': '3'}, {'name': 'lenovo', 'price': '3000', 'count': '10'}, {'name': 'tesla', 'price': '1000000', 'count': '10'}, {'name': 'chicken', 'price': '200', 'count': '1'}]

    求单价大于10000的商品格式同上

    f = open(r'shopping.txt','r',encoding='utf-8')
    res = map(lambda x:x.strip('
    ').split(','),f)
    res10000 = filter(lambda x:float(x[1])>10000,res)
    msg = map(lambda x:{'name':x[0],'price':x[1],'count':x[2]},res10000)
    msg_list=[i for i in msg]
    print(msg_list)
    f.close()
    #[{'name': 'mac', 'price': '20000', 'count': '3'}, {'name': 'tesla', 'price': '1000000', 'count': '10'}]

    函数的递归:

    二分法:

    想从一个从小到大排列的数字列表中找到指定的数字,遍历的效率太低,用二分法(算法的一种)

    实现in的效果

    def in2(num,li):
        if len(li) ==0:
            print('no exit')
            return False
        mid_ind = len(li)//2
        if num>li[mid_ind]:
            li = li[mid_ind+1:]
            return in2(num,li)
        elif num < li[mid_ind]:
            li = li[:mid_ind]
            return in2(num,li)
        else:
            return True
    
    print(in2(33,l))
    #True

    实现l.index(30)效果

    l=[1,2,3,4,5,6,7,8,11,33,44,55,66,77,88,99]
    def index2(num,li,start =0,stop=len(l)-1):
        if start<stop:
            mid_index = start+(stop-start)//2
            if num > li[mid_index]:
                start = mid_index+1
                return index2(num,li,start,stop)
            elif num < li[mid_index]:
                stop=mid_index-1
                return index2(num,li,start,stop)
            else:
                return mid_index
        else:
            print('no exist')
            return None
    
    res =index2(33,l)
    print(res)
    #9
  • 相关阅读:
    BZOJ1880: [Sdoi2009]Elaxia的路线(最短路)
    「BZOJ1433」[ZJOI2009] 假期的宿舍(二分图,网络流)
    BZOJ 1061 [Noi2008]志愿者招募(费用流)
    [BZOJ2879][Noi2012]美食节(费用流)
    bzoj 1834 [ZJOI2010] network 网络扩容(费用流)
    BZOJ2668:[CQOI2012]交换棋子(费用流)
    bzoj1070【SCOI2007】修车(费用流)
    【BZOJ 1877】 [SDOI2009]晨跑(费用流)
    SQLite_Home
    GeoMesa-单机搭建
  • 原文地址:https://www.cnblogs.com/msj513/p/9767219.html
Copyright © 2020-2023  润新知