# 作业:
# 1、文件内容如下,标题为:姓名,性别,年纪,薪资
# egon male 18 3000
# alex male 38 30000
# wupeiqi female 28 20000
# yuanhao female 28 10000
# 要求:
# 从文件中取出每一条记录放入列表中,
# 列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式
'''
keys=['name','sex','age','salary']
l=[]
dic={}.fromkeys(keys,None)
dic1={}.fromkeys(keys,None)
dic2={}.fromkeys(keys,None)
dic3={}.fromkeys(keys,None)
def info():
with open('db.txt',mode='rt',encoding='utf-8')as f:
f.__iter__()
line=f.__next__()
dic['name'], dic['sex'], dic['age'], dic['salary'] = line.strip().split(' ')
l.append(dic)
line1 = f.__next__()
dic1['name'], dic1['sex'], dic1['age'], dic1['salary'] = line1.strip().split(' ')
l.append(dic1)
line2 = f.__next__()
dic2['name'], dic2['sex'], dic2['age'], dic2['salary'] = line2.strip().split(' ')
l.append(dic2)
line3 = f.__next__()
dic3['name'], dic3['sex'], dic3['age'], dic3['salary'] = line3.strip().split(' ')
l.append(dic3)
info()
print(l)
'''
# 2 根据1得到的列表,取出薪资最高的人的信息
'''
l=[
{'name': 'egon', 'sex': 'male', 'age': '18', 'salary': '3000'},
{'name': 'alex', 'sex': 'male', 'age': '38', 'salary': '30000'},
{'name': 'wupeiqi', 'sex': 'female', 'age': '28', 'salary': '20000'},
{'name': 'yuanhao', 'sex': 'female', 'age': '28', 'salary': '10000'}
]
new_l=[]
for i in l:
new_l.append(i['salary'])
res=max(new_l)
print(res)
for j in l:
if int(j['salary']) == 30000:
print(j)
'''
# 3 根据1得到的列表,取出最年轻的人的信息
'''
l=[
{'name': 'egon', 'sex': 'male', 'age': '18', 'salary': '3000'},
{'name': 'alex', 'sex': 'male', 'age': '38', 'salary': '30000'},
{'name': 'wupeiqi', 'sex': 'female', 'age': '28', 'salary': '20000'},
{'name': 'yuanhao', 'sex': 'female', 'age': '28', 'salary': '10000'}
]
new_l=[]
for i in l:
new_l.append(i['age'])
res=min(new_l)
print(res)
for j in l:
if int(j['age']) == 18:
print(j)
'''
# 4、将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写
'''
names=['egon','alex_sb','wupeiqi','yuanhao']
name=[]
for i in names:
name.append(i.upper())
print(name)
'''
# 5、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
'''
names=['egon','alex_sb','wupeiqi','yuanhao']
name=[]
for i in names:
if i.endswith('sb'):
pass
else:
name.append(i)
print(name)
'''
# 6、求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
'''
l=[]
with open('a.txt',mode='rt',encoding='utf-8')as f:
for line in f:
l.append(len(line))
print(l,max(l))
'''
# 7、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
'''
l=[]
with open('a.txt',mode='rt',encoding='utf-8')as f:
for line in f:
l.append(len(line))
print(l,sum(l))
'''
# 8、思考题
# with open('a.txt') as f:
# g=(len(line) for line in f)
# print(sum(g)) #为何报错?
'''
with open('a.txt') as f:
g=(len(line) for line in f)
print(sum(g))
g是生成器
'''
# 9、文件shopping.txt内容如下
# mac,20000,3
# lenovo,3000,10
# tesla,1000000,10
# chicken,200,1
# 求总共花了多少钱?
'''
dic={}
l=[]
with open('shopping.txt',mode='rt',encoding='utf-8')as f:
for line in f:
name,amount,count=line.strip().split(',')
dic[name]=[int(amount),int(count)]
print(dic)
for i in dic:
counts=dic[i][0] * dic[i][1]
l.append(counts)
print(l,sum(l))
'''
# 打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
'''
keys=['name','price','count']
l=[]
dic={}.fromkeys(keys,None)
dic1={}.fromkeys(keys,None)
dic2={}.fromkeys(keys,None)
dic3={}.fromkeys(keys,None)
def info():
with open('shopping.txt',mode='rt',encoding='utf-8')as f:
f.__iter__()
line=f.__next__()
dic['name'], dic['price'], dic['count'] = line.strip().split(',')
l.append(dic)
line1 = f.__next__()
dic1['name'], dic1['price'], dic1['count'] = line1.strip().split(',')
l.append(dic1)
line2 = f.__next__()
dic2['name'], dic2['price'], dic2['count'] = line2.strip().split(',')
l.append(dic2)
line3 = f.__next__()
dic3['name'], dic3['price'], dic3['count'] = line3.strip().split(',')
l.append(dic3)
info()
print(l)
'''
# 求单价大于10000的商品信息,格式同上
'''
l=[
{'name': 'mac', 'price': '20000', 'count': '3'},
{'name': 'lenovo', 'price': '3000', 'count': '10'},
{'name': 'tesla', 'price': '1000000', 'count': '10'},
{'name': 'chicken', 'price': '200', 'count': '1'}
]
new_l=[]
for i in l:
if int(i['price']) > 10000:
new_l.append(i)
print(new_l)
'''
# 10、思考:判断下述说法是否正确
# 题目1:
# 1、应该将程序所有功能都扔到一个模块中,然后通过导入模块的方式引用它们 错误
# 2、应该只将程序各部分组件共享的那一部分功能扔到一个模块中,然后通过导入模块的方式引用它们 错误
#
# 题目2:
# 运行python文件与导入python文件的区别是什么?
'''
运行python文件
打开文件
将内容从硬盘读到内存
python解释器读取运行
导入python文件
运行python文件
产生文件的的名称空间,将文件运行过程中产生的名字都丢到文件的名称空间中
在当前文件中产生一个名字,该名字指向之前产生的名称空间
'''
# 运行的python文件产生的名称空间何时回收,为什么?
# 程序结束后
# 导入的python文件产生的名称空间何时回收,为什么?
# 引用结束后