2018.10.10作业

# 1、将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写
# names = ['egon', 'alex_sb', 'wupeiqi', 'yuanhao']
# name = [a.upper() for a in names]
# print(name)
# 2、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
# names = ['egon', 'alex_sb', 'wupeiqi', 'yuanhao']
# name2 = [len(a) for a in names if not a.endswith('sb')]
# print(name2)
# 3、求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
# with open('a.txt', 'r', encoding='utf-8')as f:
# print(max(len(lines) for lines in f))
# 4、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
# with open('a.txt', 'r', encoding='utf-8')as f:
# print(sum(len(lines) for lines in f))
# 5、思考题
#
# with open('a.txt') as f:
# g=(len(line) for line in f)
# print(sum(g))
# 为何报错?
# 对已关闭的文件进行了操作
# 6、文件shopping.txt内容如下
#
# mac,20000,3
# lenovo,3000,10
# tesla,1000000,10
# chicken,200,1
# 求总共花了多少钱?
# with open('shopping.txt', 'r')as f:
# count = 0
# for lines in f:
# list1 = lines.split(',')
# count += int(list1[1]) * int(list1[2])
# print(count)
# 打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
# shopping_dic = []
# with open('shopping.txt', 'r')as f:
# for lines in f:
# list1 = lines.split(',')
# message1 = list1[2][0:len(list1[2]) - 1]
# shopping_dic.append({'name': list1[0], 'price': list1[1], 'count': message1})
# print(shopping_dic)
# 求单价大于10000的商品信息,格式同上
# shopping_dic = []
# with open('shopping.txt', 'r')as f:
# for lines in f:
# list1 = lines.split(',')
# message1 = list1[2][0:len(list1[2]) - 1]
# if int(list1[1]) > 10000:
# shopping_dic.append({'name': list1[0], 'price': list1[1], 'count': message1})
# print(shopping_dic)

# 查找一个值是否存在于列表中并返回索引
# list1 = [1, 2, 10, 30, 33, 99, 101, 200, 301, 402]
#
#
# def search(num, l, start=0, stop=len(list1) - 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 in', mid)
# return
# search(num, l, start, stop)
# else:
# print('not exists')
# return
# search(301, list1)
posted @ 2018-10-10 15:29  Darius_Zhou  阅读(122)  评论(0)    收藏  举报