Day16作业
# 作业:
# 1、文件内容如下,标题为:姓名,性别,年纪,薪资
# egon male 18 3000
# alex male 38 30000
# wupeiqi female 28 20000
# yuanhao female 28 10000
#
# 9、文件shopping.txt内容如下
#
# mac,20000,3
# lenovo,3000,10
# tesla,1000000,10
# chicken,200,1
# 10、文件内容如下,标题为:姓名,性别,年纪,薪资
# egon male 18 3000
# alex male 38 30000
# wupeiqi female 28 20000
# yuanhao female 28 10000
# while True:
# a = input('').strip()
# b = input('').strip()
# c = input('').strip()
# d = input('').strip()
# with open('b.txt','a',encoding='utf-8') as f:
# res = f'{a} {b} {c} {d}\n'
# f.write(res)
# print('111')
# 要求:
# 从文件中取出每一条记录放入列表中,
# 列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式
#
# with open('a.txt') as f:
# for line in f:
# items = line.split()
# for i in items:
# print(i)
# info = [{'name': name, 'sex': sex, 'age': age, 'salary': salary}]
# print(info)
# d = {}
# l = []
# with open('a.txt', 'r', encoding='utf-8') as f:
# for line in f:
# name, sex, age, salary = line.split()
# d['name'] = name
# d['sex'] = sex
# d['age'] = age
# d['salary'] = salary
# res = d.copy()
# # print(d)
# l.append(res)
# print(l)
# print(line)
# print(max(salaries, key=lambda k:salaries[k]))
# 2 根据1得到的列表,取出薪资最高的人的信息
# print(max(l,key=lambda dic:dic['salary']))
# 3 根据1得到的列表,取出最年轻的人的信息
# print(min(l, key=lambda dic:dic['age']))
# 4、将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写
# names = []
# for i in l:
# name=(i['name'])
# name = name.upper()
# names.append(name)
# print(names)
# 5、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
# names = []
# for i in l:
# names.append(i['name'])
# names=[len(name) for name in names if not name.endswith('sb')]
# print(names)
# print(names)
# res=filter(lambda x:x.endswith('sb'),names)
# print(list(res))
# 5、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
#看错题目版,将以sb结尾的名字过滤掉,然后保存名字长度
# names = []
# for i in l:
# names.append(i['name'])
# str = []
# for name in names:
# if name.endswith('sb'):
# a = name
# str.append(a)
# res = len(name)
# str.append(res)
# # print(str)
# for i in range(len(names)):
# if names[i] == str[0]:
# names[i]= str[1]
# print(names)
# 6、求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
#
# with open('a.txt', 'r', encoding='utf-8') as f:
# print(max(len(line) for line in f))
# 7、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
#
# with open('a.txt', 'r', encoding='utf-8') as f:
# # res = f.read()
# # print(len(res))
# print(sum(len(line) for line in f))
# print(f.tell())
# print(sum(len(line) for line in f))
# print(f.tell())
# print(sum(len(line) for line in f))
# print(f.tell())
# f.seek(0,2)
# print(f.tell())
# 思考为何在第一次之后的n次sum求和得到的结果为0?
# 第一次之后文件指针移动到了最末尾没有东西可读了
# 8、思考题
#
# with open('a.txt') as f:
# g=(len(line) for line in f)
# print(sum(g)) #为何报错?
#g是生成器,sum不能计算单个值
# 9、文件shopping.txt内容如下
#
# mac,20000,3
# lenovo,3000,10
# tesla,1000000,10
# chicken,200,1
# 求总共花了多少钱?
# m = 0
# with open('shopping.txt', 'r', encoding='utf-8') as f:
# for line in f:
# name, price, number = line.strip('\n').split(',')
# price = int(price)
# number = int(number)
#
# money = price * number
# m += money
# print(m)
# 打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
#
# news = {}
# l = []
# with open('shopping.txt', 'r', encoding='utf-8') as f:
# for line in f:
# name, price, number = line.strip('\n').split(',')
# news['name'] = name
# news['price'] = price
# news['count'] = number
# res = news.copy()
# # print(news)
# l.append(res)
# print(l)
# 求单价大于10000的商品信息,格式同上
#
# news = {}
# l = []
# with open('shopping.txt', 'r', encoding='utf-8') as f:
# for line in f:
# name, price, number = line.strip('\n').split(',')
# price = int(price)
# if price > 10000:
# news['name'] = name
# news['price'] = price
# news['count'] = number
# res = news.copy()
# # print(news)
# l.append(res)
# print(l)
#
# 10、文件内容如下,标题为:姓名,性别,年纪,薪资
# egon male 18 3000
# alex male 38 30000
# wupeiqi female 28 20000
# yuanhao female 28 10000
#
# 要求:
# 从文件中取出每一条记录放入列表中,
# 列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式
#
# d = {}
# l = []
# with open('a.txt', 'r', encoding='utf-8') as f:
# for line in f:
# name, sex, age, salary = line.split()
# d['name'] = name
# d['sex'] = sex
# d['age'] = age
# d['salary'] = salary
# res = d.copy()
# # print(d)
# l.append(res)
# print(l)
# 根据1得到的列表,取出所有人的薪资之和
# a = 0
# for i in l:
# res = int(i['salary'])
# a += res
# print(a)
# 根据1得到的列表,取出所有的男人的名字
# for i in l:
# if i['sex'] == 'male':
# print(i['name'])
# 根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式
# l1 = []
# for i in l:
# i['name'] = i['name'].capitalize()
# l1.append(i)
# print(l1)
# 根据1得到的列表,过滤掉名字以a开头的人的信息
#
# l2 = []
# for i in l:
# if i['name'].startswith('a'):
# pass
# else:
# l2.append(i)
# print(l2)
# 11、使用递归打印斐波那契数列(前两个数的和得到第三个数,如:0 1 1 2 3 4 7...)
# def d1(a, b):
# c = a + b
# print(a,end=' ')
# d1(b, c)
# d1(0, 1)
# 12、一个嵌套很多层的列表,如l=[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]],用递归取出所有的值
#
# l=[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]]
# def demo(li):
# for i in li:
# if type(i) is list:
# demo(i)
# else:
# print(i)
# demo(l)
# 13、思考:判断下述说法是否正确
# 题目1:
# 1、应该将程序所有所有所有的功能都扔到一个模块中,然后通过导入模块的方式引用它们 X
# 2、应该只将程序各部分组件共享的那一部分功能扔到一个模块中,然后通过导入模块的方式引用它们 √
#
# 题目2:
# 运行python文件与导入python文件的区别是什么?
#运行是将文件打开然后读入内存,解释器从内存读取运行,导入是运行文件产生一个名称空间,产生一个指向其的名字
# 运行的python文件产生的名称空间何时回收,为什么?
#程序结束后回收
# 导入的python文件产生的名称空间何时回收,为什么?
#对导入python文件的引用结束后回收
# 14、运行run.py,然后在run.py中导入了模块m1、m2,请画出内置名称空间、各个全局名称空间的嵌套关系图