# 四 聲明式編程練習題# 一、將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)# 二、將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))# 三、求文件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('\n')# 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))# 四、求文件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?# 五、思考題# 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)時循環多個值## 六、文件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('\n').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('\n').split(',')[0],# 'price':int(line.strip('\n').split(',')[1]),# 'count':int(line.strip('\n').split(',')[2])} for line in f]# print(info)# with open('shopping.txt',encoding='utf-8') as f:# info = [{'name': line.strip('\n').split(',')[0],# 'price': int(line.strip('\n').split(',')[1]),# 'count': int(line.strip('\n').split(',')[2])} for line in f if int(line.strip('\n').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)