• 作业八.


    1.将names = ['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写

    names = ['egon','alex_sb','wupeiqi','yuanhao']
    new_names = []
    new_names = [name.upper() for name in names]
    print(new_names)


    2.将names = ['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字
    names = ['egon','alex_sb','wupeiqi','yuanhao']
    names = [name for name in names if not name.endswith('sb')]
    print(names)


    3.求文件中a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
    with open('a.txt',encoding='utf-8') as f:
        print(max(len(line) for line in f))

    4.求文件a.txt中总共包含的字符个数.思考为何在第一次之后的n次sum求和得到的结果为0?
    with open('a.txt',encoding='utf-8') as f:
        print(sum(len(line) for line in f))

    5.思考题
    因为关闭了文件上的io 操作


    6.文件shopping.txt内容如下
    mac,20000,3
    lenovo,3000,10
    tesla,1000000,10
    chicken,200,1
    求总共花了多少钱?
    打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3..}]
    求单价大于10000的商品信息,格式同上


    with open('shopping.txt','rt',encoding='utf-8') as f:
    info = [line.split()for line in f]
    a = sum(float(p)*int(c) for p,c in info)
    print(a)


    with open('shopping.txt','rt',encoding='utf-8') as f:
    for line in f:
    a = [{'name':line.strip(' ').split(',')[0],
    'price':line.strip(' ').split(',')[1],
    'count':line.strip(' ').split(',')[2]}]
    print(a)


    with open('shopping.txt','rt',encoding='utf-8') as f:
    for line in f:
    a = [{'name':line.strip(' ').split(',')[0],
    'price':line.strip(' ').split(',')[1],
    'count':line.strip(' ').split(',')[2]}]
    if int(line.strip(' ').split(',')[1]) > 10000:
    print(a)


    6.
    l=[1,2,10,30,33,99,101,200,301,311,402,403,500,900,1000]
    
    def search(n,l):
        print(l)
        if len(l) == 0:
            print('not exists')
            return
        mid_index=len(l) // 2
        if n > l[mid_index]:
            l=l[mid_index+1:]
            search(n,l)
        elif n < l[mid_index]:
            l=l[:mid_index]
            search(n,l)
        else:
            print('find it')
    
    
    search(3,l)



    l=[1,2,10,30,33,99,101,200,301,402]
    
    def search(num,l,start=0,stop=len(l)-1):
        if start <= stop:
            mid=start+(stop-start)//2
            print('start:[%s] stop:[%s] mid:[%s] mid_val:[%s]' %(start,stop,mid,l[mid]))
            if num > l[mid]:
                start=mid+1
            elif num < l[mid]:
                stop=mid-1
            else:
                print('find it',mid)
                return
            search(num,l,start,stop)
        else:
            print('not exists')
            return
    
    search(301,l)








  • 相关阅读:
    Android 废弃方法属性解决
    Android RecycleView分组
    Android 第三方库FlycoTabLayout
    Android 自定义dialog出现的位置
    Android 底部弹窗实现
    Android 自定义设置布局
    Android 微信、qq分享文本 (Intent)
    SpringBoot关于跨域的三种解决方案
    记录一次通用Mapper+自定义mapper出现的问题分析以及排查
    IDEA配置Maven+新建Maven项目
  • 原文地址:https://www.cnblogs.com/lvyipin1/p/9769379.html
Copyright © 2020-2023  润新知