Anonymous function exercises

  1. 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
info_list=[]
keys = ['name','sex', 'age', 'salary']
with open('test.txt','r',encoding='utf8')as fr:
    for i in fr:
        values = i.split()
        res = zip(keys, values)
        info_dict = {k: v for k, v in res}

        info_list.append(info_dict)

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

    res1 = max(info_list, key=lambda salary: salary['salary'])
    print(res1)
    
  2. According to a list obtained by removing most of the young people's information

    res2=min(info_list,key=lambda age:age['age'])
    print(res2)
    
  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

    res4=filter(lambda name: name['name'].startswith('a'), info_list)
    print(list(res4))
    
  5. Print recursive Fibonacci number (numbers and the first two to give a third number, such as: 0112347 ...)

    def f1(n):
        if n<=1:
            return n
        else:
            return (f1(n - 1) + f1(n - 2))
    lis=[]
    for i in range(6):
        lis.append(f1(i))
    print(lis)
  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

Guess you like

Origin www.cnblogs.com/jinhongquan/p/11587467.html