zoukankan      html  css  js  c++  java
  • PYTHON-匿名函数,递归与二分法,面向过程编程-练习

    # 四 声明式编程练习题

    # 1、将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写
    names = ['egon', 'alex_sb', 'wupeiqi', 'yuanhao']
    # # 方式一:手动实现
    # new_names=[]
    # for line in names:
    # new_names.append(line.swapcase())
    # print(new_names)
    #
    # # 方式二:列表生成式
    # new_names=[line.swapcase() for line in names]
    # print(new_names)
    #
    # # 方式三:map+匿名函数
    # res=map(lambda line :line.swapcase() ,names)
    # print(list(res))

    # names=[name.upper() for name in names]
    # print(names)

    # 2、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
    names = ['egon', 'alex_sb', 'wupeiqi', 'yuanhao']

    # new_names=[]
    # for line in names:
    # if not line.endswith('sb'):
    # new_names.append(line)
    # print(new_names)

    # new_names=[len(line) for line in names if not line.endswith('sb') ]
    # print(new_names)

    # res=filter(lambda line:not line.endswith('sb'),names)
    # print(list(res))

    # 3、求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
    # file=[]
    # with open('a.txt','rt',encoding='utf-8')as f:
    # for line in f:
    # res=f.read()
    # # print(res)
    # res=res.split(' ')
    # file.append(res)
    # print(file)
    # def funcs(x):
    # return len(x)
    # res=max(file,key=funcs)
    # print(res)
    # res2=max(res,key=funcs)
    # print(len(res2))

    # print(max(len(line) for line in f))

    # 4、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
    # with open('a.txt','rt',encoding='utf-8')as f:
    # print(sum(len(line) for line in f))
    # print(sum(len(line) for line in f)) # 求包换换行符在内的文件所有的字符数,为何得到的值为0?

    # 5、思考题
    # with open('a.txt') as f:
    # g=(len(line) for line in f)
    # print(sum(g)) #为何报错?

    # with open('a.txt') as f:
    # print(sum(len(line) for line in f))
    # print(sum(g)) #为何报错?(len(line) for line in f)时循环多个值
    #
    # 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.strip(' ').split(',') for line in f]
    # print(info)
    # cost=sum(float(unit_price) *int(count) for _,unit_price,count in info)
    # print(cost)


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


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


    # ====================
    # 函数递归
    # 四 二分法
    #
    # 想从一个按照从小到大排列的数字列表中找到指定的数字,
    # 遍历的效率太低,用二分法(算法的一种,算法是解决问题的方法)可以极大低缩小问题规模
    # 实现类似于in的效果
    # l = [1, 2, 10, 30, 33, 99, 101, 200, 301, 311, 402, 403, 500, 900, 1000] # 从小到大排列的数字列表
    # # find_index = 900
    # def search(find_index, l):
    # print(l)
    # if len(l)==0:
    # print('not find!')
    # return
    # mid_num = len(l) // 2
    # mid_index = l[mid_num]
    # # print(mid_index)
    # if find_index > mid_index:
    # print('right')
    # l = l[mid_num + 1:]
    # search(find_index, l)
    # elif find_index < mid_index:
    # print('left')
    # l = l[0:mid_num]
    # search(find_index, l)
    # else:
    # print('find it!')
    #
    # search(901, l)

    # 实现类似于l.index(30)的效果
    # 查找一个值是否存在于列表中并返回索引
    # l = [1, 2, 10, 30, 33, 99, 101, 200, 301, 311, 402, 403, 500, 900, 1000] # 从小到大排列的数字列表
    # 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: #如果stop > start则意味着列表实际上已经全部切完,即切为空
    # print('not exists')
    # return
    #
    # search(301,l)
  • 相关阅读:
    Windows 显示隐藏文件
    Python 程序一行代码解决乘法口诀表
    【转发】基于Bert-NER构建特定领域的中文信息抽取框架(上)
    【转发】GET和POST两种基本请求方法的区别
    【转发】实现yolo3模型训练自己的数据集总结
    第十章集合总结
    2016-2017 201671010134 异常处理
    JAVA基础编程
    2016-2017 201671010134 第六章总结
    java总结
  • 原文地址:https://www.cnblogs.com/du-jun/p/9769165.html
Copyright © 2011-2022 走看看