一、题目:
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}
的形式
2. 根据1得到的列表,取出薪资最高的人的信息
3. 根据1得到的列表,取出最年轻的人的信息
4. 根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式
5. 根据1得到的列表,过滤掉名字以a开头的人的信息
6. 使用递归打印斐波那契数列(前两个数的和得到第三个数,如:0 1 1 2 3 4 7...)
l1 = [] l2 = ['name', 'sex', 'age', 'salary'] with open('homework.py') as f: for i in f: res = i.strip().split(' ') user_dict = {k: v for k, v in zip(l2, res)} l1.append(user_dict) print(l1)
with open('homework.py') as f: items = (line.split() for line in f) info = [{'name': name, 'sex': sex, 'age': age, 'salary': salary} for name, sex, age, salary in items] print(info)
2.
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'}] rich = max(l, key = lambda i: i['salary']) print(rich)
3.
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'}] young = min(l, key = lambda i: i['age']) print(young)
4.
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 = filter(lambda i: not i['name'].startswith('a'), l) print(list(info))
5.
def outer(): a = 0 b = 1 c = 0 def inner(): nonlocal a, b, c print(a) c = a + b b = a a = c inner() return inner outer()()
a = 0 b = 1 c = 0 def x(): t = 0 while t < 100: global a, b, c print(a) c = a + b b = a a = c t += 1 x()
a = 0 b = 1 c = 0 def x(): global a, b, c print(a) c = a + b b = a a = c x() x()
x = 0 y = 1 count = 0 while count < 10: print(x) x, y = y, x + y count += 1
def func(x, y, end=999): if x > end: return
6.
l = [1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]] def x(): global l for i in l: if isinstance(i, int): print(i) if isinstance(i, list): l = l.pop() x() x()