Fork me on GitHub
# 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'}
]
sum=0
for i in l:
salary=i['salary']
salary=int(salary)
sum += salary
print(sum)
'''
# 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:
name=i['name']
new_l.append(name)
print(new_l)
'''
# 4 根据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'}
]
for i in l:
old=i['name'][0]
new=i['name'][0].upper()
new_l=i['name'].replace(old,new)
print(new_l)
old_l=i['name']
print(old_l)
i['name']=new_l
print(l)
'''
# 5 根据1得到的列表,过滤掉名字以a开头的人的信息
'''
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:
name=i['name']
print(name)
if name.startswith('a'):
print(name)
else:
new_l.append(i)
print(new_l)
'''
# 6 使用递归打印斐波那契数列(前两个数的和得到第三个数,如:0 1 1 2 3 4 7...)
'''
def f(n):
if n == 1:
return 0
elif n == 2:
return 1
elif n == 3:
return 1
else:
return f(n - 1) + f(n - 2)
for n in range(1,10):
print(f(n))
'''
# 7 一个嵌套很多层的列表,如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 f(lists):
for x in lists:
if type(x) is list:
f(x)
else:
print(x)

f(l)
'''
posted on 2020-03-25 17:16  OBOS  阅读(179)  评论(0编辑  收藏  举报