作业:
# 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}的形式
ANSR:
info_l = []
with open("a.txt", mode="rt", encoding="utf-8") as f:
for line in f:
info_d = {}
info_d['name'], info_d['sex'], info_d['age'], info_d['salary'] = line.strip("\n").split(" ")
info_l.append(info_d)
print(info_l)
# 2 根据1得到的列表,取出薪资最高的人的信息
ANSR:
info_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'}]
salary_d = {}
for item in range(len(info_l)):
# print(item,info_l[item]['salary'])
salary_d[info_l[item]['salary']] = item
index = salary_d[max(salary_d)]
salary_max = info_l[index]
print(salary_max) # {'name': 'alex', 'sex': 'male', 'age': '38', 'salary': '30000'}
# 3 根据1得到的列表,取出最年轻的人的信息
ANSR:
info_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'}]
age_d = {}
for item in range(len(info_l)):
# print(item,info_l[item]['age'])
age_d[info_l[item]['age']] = item
index = age_d[min(age_d)]
age_min = info_l[index]
print(age_min) # {'name': 'egon', 'sex': 'male', 'age': '18', 'salary': '3000'}
# 4 根据1得到的列表,取出所有人的薪资之和
ANSR:
info_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'}]
salary_sum = sum(int(info_l[item]["salary"]) for item in range(len(info_l)))
print(salary_sum)
# 5 根据1得到的列表,取出所有的男人的名字
ANSR:
info_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'}]
name_male = [info_l[item]["name"] for item in range(len(info_l)) if info_l[item]['sex'] == "male"]
print(name_male) # ['egon', 'alex']
# 6 根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式
ANSR:
info_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'}]
for item in range(len(info_l)):
info_l[item]["name"] = info_l[item]['name'].title()
print(info_l)
# 7 根据1得到的列表,过滤掉名字以a开头的人的信息
ANSR:
info_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'}]
info_l = [info_l[item] for item in range(len(info_l)) if not info_l[item]['name'].startswith("a")]
print(info_l)
# 8、将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写
ANSR:
names = ['egon', 'alex_sb', 'wupeiqi', 'yuanhao']
names = [i.upper().replace("SB","sb") for i in names]
print(names)
# 9、将names=['egon','alex_sb','wupeiqi','yuanhao']中以sb结尾的名字过滤掉,然后保存剩下的名字长度
ANSR:
names = ['egon', 'alex_sb', 'wupeiqi', 'yuanhao']
names = [i for i in names if not i.endswith("sb")]
print(names)
# 10、求文件a.txt中最长的行的长度(长度按字符个数算,需要使用max函数)
ANSR:
with open("a.txt", mode="rt", encoding="utf-8") as f:
item = (len(line) for line in f)
print(max(item))
# 11、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
ANSR:
with open("a.txt", mode="rt", encoding="utf-8") as f:
nums = (len(line) for line in f)
sums = sum(nums)
# 12、思考题
with open('a.txt') as f:
g=(len(line) for line in f)
print(sum(g)) #为何报错?
ANSR:
文件对象必须是打开状态的,关闭状态的无法调用生成器,缩进至with语句下即可正常。
# 13、文件shopping.txt内容如下
mac,20000,3
lenovo,3000,10
tesla,1000000,10
chicken,200,1
求总共花了多少钱?
打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
ANSR:
# 求和
with open("shopping.txt", mode="rt", encoding="utf-8") as f:
g=(int(line.strip("\n").split(",")[1])*int(line.strip("\n").split(",")[2]) for line in f)
print(sum(g)) # 10090200
# 打印所有商品的信息
with open("shopping.txt", mode="rt", encoding="utf-8") as f:
item = (line.strip("\n").split(",") for line in f) # 一个生成器
info = [{'name': name, 'price': price, 'count': count} for name, price, count in item] # 列表生成式
print(info)
# 14、使用递归打印斐波那契数列(前两个数的和得到第三个数,如:0 1 1 2 3 4 7...)
ANSR:
list = []
for i in range(20):
if i == 0:
list.append(0)
elif i == 1:
list.append(1)
else:
list.append(list[i-2]+list[i-1])
print(list)
# 15、一个嵌套很多层的列表,如l=[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]],用递归取出所有的值
ANSR:
l = [1, 2, [3, [4, 5, 6, [7, 8, [9, 10, [11, 12, 13, [14, 15]]]]]]]
def func(l):
for i in l:
if isinstance(i,list):
func(i)
else:
print(i, end=" ")
func(l)