程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[weekly Python 100 day question brushing plan] day3~ skillfully use several important built-in functions

編輯:Python

Catalog

Write it at the front

Topic 1

         analysis

         answer

Topic two

         analysis

         answer

️ Topic three

        ️ analysis

        ️ answer

Topic four

         analysis

         answer

  summary


Write it at the front

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 !

Topic 1

use map To handle string lists name=['lisa','wu,'yua','zhou'], Put everyone on the list , All become names plus ‘_nb’, example lisa_nb 

analysis

You need to master the built-in before writing this question map function , Anonymous functions , And the use of various list derivations

answer

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 :

Topic two

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

analysis

Before writing this question, you need to master the built-in functions filter、 list 、 Anonymous functions 、 How to use the list derivation

answer

​
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 :

️ Topic three

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

️ analysis

Before writing this question, you need to master the built-in functions divmod、if The use of conditional statements

️ answer

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 :

Topic four

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

 

analysis

Writing this question requires mastering The basics of dictionaries , Built in functions filter The basic use method

answer

(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 :

  summary

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 ”.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved