Built-in function and recursive

1. The content of the file is as follows, the title is: name, gender, age, salary

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

Requirements:
Take out each record from the file and put it into the list. Each element of the list is {'name':'egon','sex':'male','age ':18,'salary':3000}

  1. According to the list obtained by 1, take out the information of the person with the highest salary
  2. According to 1 List, take out the information of the youngest person
  3. According to the list obtained by 1, map the name of each person’s information to the form of initial capital letters
  4. According to the list obtained by 1, Filter out the information of people whose names start with a
  5. Use recursion to print the Fibonacci sequence (the sum of the first two numbers gets the third number, such as: 0 1 1 2 3 4 7… )
  6. A list with many levels of nesting, such as l=[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13] ,[14,15]]]]]]], use recursion to fetch all the values
#Remove each record from the file and put it into the list. Each element is in the form of `{'name':'egon','sex':'male','age':18,'salary':3000}` with open(r'info.txt','r ',encoding='utf-8') as fr: values_info = fr.read().split("
")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. According to the list obtained in 1, take out the information of the person with the highest salary print(max(values_list, key=lambda salary:salary[" salary"]))# 2. According to the list obtained in 1, take out the information of the youngest person print(max(values_list, key=lambda age:age["age"]))# 3. According to the list obtained in 1, set The name in each person’s information is mapped to the initial capitalized form def func(item): item['name'] = item['name'].capitalize() return itemres = list(map(func,values_list))print (res)# 4. According to the list obtained by 1, filter out the information of the person whose name starts with a def f2(item): if item['name'].startswith('a'): return False else: return Trueres = filter(f2,values_list)print(list(res))# 5. Use recursion to print the Fibonacci sequence (the sum of the first two numbers gets the third number, such as: 0 1 1 2 3 4 7...) x = 0y = 1lis1 = []def f5(): global x,y lis1.append(x) x,y = y,x+y if len(lis1)==10: return f5()f5()print( lis1)

Leave a Comment

Your email address will not be published.