The job number March 26

Job: 
1, the following contents of the file, entitled: name, sex, age, salary
Egon MALE 18 is 3000
Alex 30000 MALE 38 is
wupeiqi FEMALE 28 20000
yuanhao FEMALE 28 10000

requires:
Remove each record from the file into the list,
the list each element is { 'name': 'egon' , 'sex': 'male', 'age': 18, 'salary': 3000} form
def res():
    list1=[]
    with open(r'a.txt','r',encoding='utf-8') as f:
        for info in f:
            name,sex,age,salary = info.strip().split(' ')
            list1.append(
                {'name':name,'sex':sex,'age':age,'salary':salary}
            )
    print(list1)
list1=res()

 


2 according to Table 1 obtained, the highest-paid extracted person's information
max_salary = max(list1,key=lambda k: list1['salary'])
print(max_salary)
 
According to a list obtained by 3 1 Remove the youngest person information
min_age = min(list1,key=lambda l: list1['age'])
print(min_age)
4, the names = [ 'egon', 'alex_sb', 'wupeiqi', 'yuanhao'] in the name of all modifications caps
names=['egon','alex_sb','wupeiqi','yuanhao']
new_name = list(k.title() for k in names)
print(new_name)
 
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']
len_name = list(map(lambda name:len(name),filter(lambda name: not name.endswith('sb'),names)))
print(len_name)
 
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','r',encoding='utf-8')as f:
    res=max(len(line) for line in f)
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? (Need to use the sum function)
with open('a.txt','r',encoding='utf-8')as f:
    res=sum(len(line) for line in f)
print(res)
Pointer at the end

8 Questions

with Open ( 'a.txt') AS f:
G = (len (Line) for Line in f)
Print (SUM (G)) # Why error?
9, file shopping.txt follows

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

Print out all commodity information, the format of [{ 'name': 'xxx ', 'price': 333, 'count': 3}, ...]
def shopping():
    list2=[]
    with open('shopping.txt','r',encoding='utf-8')as f1:
        for line in f1:
            name,price,count = line.strip().split(',')
            list2.append(
                {'name':name,'price':price,'count':count
            })
    return list2
list2=shopping()
spend=sum(int(l['price'])*int(l['count']) for l in list2)
print(spend)

 


Seeking unit price of more than 10,000 product information, in the format above
price1= list(i for i in list2 if int(i['price'])>10000)
print(price1)

10 Consideration: The following statement is correct is determined
Title 1:
1, all functions should be thrown into a program module, and then they are referenced by way of introduction module
2, the program should only portions of that part of the functions of shared components throwing into a module, and then they are referenced by way of introduction module
# 1 does not, two pairs
    Topic 2: 
Run the difference between python file and python import document what is?
When the name space to run python files generated by recycling, and why?
When the name space to import python files generated by recycling, and why?
The main difference between python file and run python files on import and file name, in the name space issues
     - file name problem: 
        built-in properties runtime __name__ equal ' __main__ ' , 
        when importing __name__ equal to the file name python file. 
        
    - namespace recovery problem: 
        namespace file generated by running python recovered after the end of the file operation; 
        namespace into the python file generated when its reference count is 0 recovery.
 

 



Guess you like

Origin www.cnblogs.com/jingpeng/p/12578103.html