Catalog
Write it at the front
Topic 1
analysis
answer
Topic two
analysis
answer
️ Topic three
️ analysis
️ answer
Topic four
analysis
answer
summary
This blog mainly introduces to your friends Python Classic example < Use of several important built-in functions >, Take you to master Python Basics , I hope you can gain more knowledge here ! Let's learn together ! Progress together !
use map To handle string lists name=['lisa','wu,'yua','zhou'], Put everyone on the list , All become names plus ‘_nb’, example lisa_nb
You need to master the built-in before writing this question map function , Anonymous functions , And the use of various list derivations
Basic Edition :
name=['lisa','jadd','yua','mike'] def func(li): li = li+'_nb' return li ret = map(func,name) name = [i for i in ret] print(name)
premium :
name=['lisa','jadd','yua','mike'] ret = map(lambda li :li+'_nb',name) name = [i for i in ret] print(name)
Output results :
use filter Function to handle a list of numbers , Will list num = [1,3,5,6,7,8] All even numbers in are filtered out
Before writing this question, you need to master the built-in functions filter、 list 、 Anonymous functions 、 How to use the list derivation
num = [1,3,5,6,7,8] res = filter(lambda x:x%2 == 0,num) num2 = [i for i in res] print(num2)
Output results :
Feel free to write one with 20 Line above , It is required to run the program , First read the content into memory , Store... In a list . Receive page number input by user , each page 5 That's ok , Only output the content of the current page
Before writing this question, you need to master the built-in functions divmod、if The use of conditional statements
with open('01-- Yesterday's homework ',encoding='utf8') as f: l = f.readlines() print(l) # see file '01-- Yesterday's homework ' What's in it page_num = int(input(' Please enter the page number you want to view :')) pages,mod = divmod(len(l),5) #page Indicates the total number of pages ,mod Indicates the number of lines left ( Five actions one page ) if mod: # Determine whether there are lines left pages += 1 # Less than five lines is a page if page_num > pages: # Determine whether the number of pages entered exceeds the total number of pages print(' The page number you entered is incorrect ') elif page_num == pages and mod != 0: for i in range(mod): print(l[(page_num-1)*5 + i].strip()) else: for i in range(5): print(l[(page_num-1)*5 + i].strip())
Output results :
as follows , Every little dictionary has name Corresponding to the stock name ,shares How many shares are there ,price Corresponding to the price of the stock
portfolio=[
{’name':’IBM','shares': 100, 'price': 91.1},
{'name':’AAPL','shares’: 50,'price': 543.22},
{'name’:'FB','shares': 200,'price': 21.09},
{’name':'HPQ','shares': 35, 'price': 31.75},
{’name’ :'YHO0'.' shares': 45. 'price’: 16.35},
{’name’:'ACME’,'shares’: 75,'price': 115.65}
](1)、 Calculate the total purchase price of each stock
(2)、 use filter Filter out , The unit price is greater than 100 What are your stocks
Writing this question requires mastering The basics of dictionaries , Built in functions filter The basic use method
(1) answer :
portfolio=[ {'name':'IBM','shares':100, 'price': 91.1}, {'name':'AAPL','shares': 50,'price': 543.22}, {'name':'FB','shares': 200,'price': 21.09}, {'name':'HPQ','shares': 35, 'price': 31.75}, {'name' :'YHO0','shares': 45, 'price': 16.35}, {'name':'ACME','shares': 75,'price': 115.65} ] ret1 = map(lambda dic:{dic['name']:dic['shares']*dic['price']},portfolio) print(list(ret1))
Output results :
(2) answer :
portfolio=[ {'name':'IBM','shares':100, 'price': 91.1}, {'name':'AAPL','shares': 50,'price': 543.22}, {'name':'FB','shares': 200,'price': 21.09}, {'name':'HPQ','shares': 35, 'price': 31.75}, {'name' :'YHO0','shares': 45, 'price': 16.35}, {'name':'ACME','shares': 75,'price': 115.65} ] # The first method ret2 = filter(lambda dic:True if dic['price']>100 else False,portfolio) print(list(ret2)) # The second method ret3 = filter(lambda dic:dic['price']>100,portfolio) print(list(ret3))
Output results :
These questions are mainly about important built-in functions map、filter How to use , At the same time, master the basic syntax of anonymous functions , Be able to use flexibly in practical problems , The syntax of the anonymous function is :
Well, today's sharing is over , We “ See you tomorrow ”.