python based learning -day19 == homework exercises (+ recursive function generator expression)

Today's job:

I will do a question:

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

Claim:

1. Remove each record from the file into the list,

Each element of the list is { 'name': 'egon', 'sex': 'male', 'age': 18, 'salary': 3000} form

dict_list=[]
with open('user.txt', 'rt', encoding='utf-8') as f:
    for line in f:
        dict = {}
        name,sex,age,salary=line.strip().split(' ')
        dict['name']=name
        dict['sex']=sex
        dict['age']=age
        dict['salary']=int(salary)
        dict_list.append(dict)
​
    print(dict_list) 

The list 1 was taken out and salaries of all

sum_salary=0
for line in dict_list:
    sum_salary+=line['salary']
print(sum_salary)

The resulting list 1, remove all man's name

for line in dict_list:
    if line['sex']=='male':
        print(line['name'])

4. Table 1, we will get everyone's information in the name mapped to uppercase first letter

for line in dict_list:
   line['name']=line['name'].title()
   print(line['name'])

The resulting list 1, a person to filter out names that begin with the information

new_list=[line for line in dict_list if not line['name'].startswith('A')]
print(new_list)
​
#for line in dict_list:
   # if not line['name'].startswith('A'):
        # print(line)

6. Print recursive Fibonacci number (numbers and the first two to give a third number, such as: 0112347 ...)

def func(x,y):
       s=x+y
       print(x,end=" ")
       if y <= 100:
          func(y,s)

func(0,1)

7. a nested list of many layers, such as l = [1,2, [3, [4,5,6, [7,8, [9,10, [11,12,13, [14,15] ]]]]]], remove all the values ​​recursively

= L [1,2, [. 3, [4,5,6, [7, 8, [9, 10 are, [11,12, 13, [14, 15 are ]]]]]]]
 DEF FUNC (L) :
     for the X- in L:
         IF of the type (the X-) iS list:
 # determine if the current value is out list, the cycle continues to call itself the value 
             FUNC (the X-)
         the else :
              Print (the X-) 
FUNC (L)

 

Guess you like

Origin www.cnblogs.com/dingbei/p/12569367.html