Built-in functions and recursion

1. The document reads as follows, titled: name, sex, age, salary

egon male 18 3000
alex male 38 30000
wupeiqi female 28 20000
yuanhao female 28 10000

Requirements:
Remove each record from a file into a list, each element of the list {'name':'egon','sex':'male','age':18,'salary':3000}form

  1. The list 1 obtained, the highest-paid extracted person's information
  2. According to a list obtained by removing most of the young people's information
  3. According to Table 1, it will get everyone's information in the name mapped to uppercase first letter
  4. According to a list obtained by filtering out people whose names begin with a message
  5. Print recursive Fibonacci number (numbers and the first two to give a third number, such as: 0112347 ...)
  6. A list of many layers of nested, such as l = [1,2, [3, [4,5,6, [7,8, [9,10, [11,12,13, [14,15]]] ]]]], remove all the values ​​recursively
#从文件中取出每一条记录放入列表中,列表的每个元素都是`{'name':'egon','sex':'male','age':18,'salary':3000}`的形式

with open(r'info.txt','r',encoding='utf-8') as fr:
    values_info = fr.read().split("\n")

keys = ["name", "sex", "age", "salary"]
values_list = []
for i in range(len(values_info)):
    res = zip(keys, values_info[i].split())
    values_list.append({k: v for k, v in res})

print(values_list)

# 1.根据1得到的列表,取出薪资最高的人的信息
print(max(values_list, key=lambda salary:salary["salary"]))

# 2.根据1得到的列表,取出最年轻的人的信息
print(max(values_list, key=lambda age:age["age"]))

# 3.根据1得到的列表,将每个人的信息中的名字映射成首字母大写的形式
def func(item):
    item['name'] = item['name'].capitalize()
    return item

res = list(map(func,values_list))
print(res)

# 4.根据1得到的列表,过滤掉名字以a开头的人的信息
def f2(item):
    if item['name'].startswith('a'):
        return False
    else:
        return True
res = filter(f2,values_list)

print(list(res))

#  5.使用递归打印斐波那契数列(前两个数的和得到第三个数,如:0 1 1 2 3 4 7...)
x = 0
y = 1
lis1 = []
def f5():
    global x,y
    lis1.append(x)
    x,y = y,x+y
    if len(lis1)==10:
        return
    f5()
f5()

print(lis1)

Guess you like

Origin www.cnblogs.com/whkzm/p/11588546.html