Learning python programming ideas, built-in functions and operations Module

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 is { 'name': 'egon' , 'sex': 'male', 'age': 18, 'salary': 3000} form

with open('db.txt','rt',encoding='utf8') as f:
    user_info = []
    for line in f:
        name,sex,age,salary=line.strip().split()
        info = [name,sex,int(age),int(salary)]
        user = {k:v for k,v in zip(['name','sex','age','salary'],info)}
        user_info.append(user)

print(user_info)

2, obtained according to Table 1, the highest-paid extracted person's information

max_salary_info = max(user_info,key=lambda k:k['salary'])
print(max_salary_info)

3, according to a list obtained by removing most of the young people's information

youngest_info = min(user_info,key=lambda k:k['age'])
print(youngest_info)

4, the names = [ 'egon', 'alex_sb', 'wupeiqi', 'yuanhao'] in the name of all modifications caps

names=['egon','alex_sb','wupeiqi','yuanhao']
names1 = map(lambda name:name.upper() ,names)
names2 = (name.upper() for name in names)
print(list(names1))
print(list(names2))

5, the names = [ 'egon', 'alex_sb', 'wupeiqi', 'yuanhao'] sb end to filter out names, and store the remaining length of the name

names=['egon','alex_sb','wupeiqi','yuanhao']
sb_name = [name[:-3] for name in names if name.endswith('sb')]
print(sb_name[0])

sb_name1 = list(filter(lambda name:name.endswith('sb'),names))
print(sb_name1[0][:-3])

6, the length of the longest seek file a.txt rows (the number of characters counted by the length, it is necessary to use the max function)

with open('a.txt','rt',encoding='utf8') as f:
    max_line = max(f,key=lambda k:len(k))

res = len(max_line)
print(res)

7, the number of characters seeking a.txt file contains a total of? Thinking of the outcome after the first of the n-th sum obtained by summing up to zero? (Required sum function)
method

with open('a.txt','rt',encoding='utf8') as f:
    res = 0
    for line in f:
        res += len(line)

print(res)

Method Two

with open('a.txt','rt',encoding='utf8') as f:
    res= sum([len(line) for line in f])

print(res)

with open('a.txt','rt',encoding='utf8') as f:
    res = sum(len(line) for line in f)
    res1= sum(len(line) for line in f)

print(res)  #130
print(res1) #0

Because the file pointer is to jump to the end of the file after the first sum sum

with open('a.txt','rt',encoding='utf8') as f:
    res = sum(len(line) for line in f)
    f.seek(0,0)
    res1= sum(len(line) for line in f)

print(res)  #130
print(res1) #130

8 Questions

with open('a.txt') as f:
    g=(len(line) for line in f)
print(sum(g)) #为何报错?

g is a generator, g values ​​generated content file to be read; SUM (g) with control outside the range, an error is reported.

9, the document reads as follows shopping.txt

mac, 20000,3
lenovo, 3000,10
Tesla, 1000000,10
Chicken, 200, 1
seeking a total of how much was spent?

with open('shopping.txt','rt',encoding='utf8') as f:
    shopping_list = []
    keys = ['name','price','count']
    for line in f:
        name,price,count=line.strip().split(',')
        values=[name,int(price),int(count)]
        shopping_list.append(dict(zip(keys,values)))

total_money = sum(item['price']*item['count'] for item in shopping_list)
print(total_money)

Print out all commodity information, the format of [{ 'name': 'xxx', 'price': 333, 'count': 3}, ...]

print(shopping_list)

Seeking unit price of more than 10,000 product information, in the format above

ten_thousand_more = filter(lambda x:x.get('price')>10000,shopping_list)
print(list(ten_thousand_more))

10, thinking: judge the following statement is correct

Title 1:
1, all functions should be thrown into a program module, and then they are referenced by way of introduction module
error
thrown into a part of the functions that the module 2, the program should only portions of shared components, then import the module by way of reference them
for
2 title:
run the difference between python file and python import document what is?
Run python file will have the name of the file space, if this python file import other files python, python will produce namespace file import,
the namespace python files to be imported will generate a python file import name, namespace name points to the python import the resulting file
name space to run python file produced when recycling, and why?
After the python file recovery operation
when the namespace import python files generated by recycling, and why?
After running python file is imported recovery

Guess you like

Origin www.cnblogs.com/leilijian/p/12577239.html