作业
现有文件info.txt, 其内容如下:
alpha male 18 1000
bravo male 28 2000
charlie female 38 3000
delta female 48 4000
- 从文件中取出每一条记录放入列表中,列表的每个元素都是
{'name':'alpha','gender':'male','age':18,'salary':1000}
的形式 - 根据1得到的列表,取出薪资最高的人的信息
- 根据1得到的列表,取出最年轻的人的信息
- 根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式
- 根据1得到的列表,过滤掉名字以a开头的人的信息
# 1
with open('info.txt', 'rt', encoding='utf-8') as f:
items = [line.split() for line in f]
info_list = [{'name': name, 'gender': gender, 'age': age, 'salary': salary} /
for name, gender, age, salary in items]
print(info_list)
# 2
max_salary_info = max(info_list, key=lambda dic: dic['salary'])
print(max_salary_info)
# 3
min_age_info = min(info_list, key=lambda dic: dic['age'])
print(min_age_info)
# 4
new_info_list = map(lambda dic: {'name': dic['name'].capitalize(),
'gender': dic['gender'],
'age': dic['age'],
'salary': dic['salary']}, info_list)
print(list(new_info_list))
# 5
res = filter(lambda dic: dic['name'][0] != 'a', info_list)
print(list(res))
斐波那契数列
num1 = 0
num2 = 1
num = 0
def fibo(end):
'''斐波那契数列'''
global num1, num2, num
print(num)
num1 = num2
num2 = num
num = num1 + num2
if num > end:
return
func(end)
func(20)