Built-in functions:
Homework:
3. Use map to process the list of strings and turn everyone in the list into SB, for example ALEX_SB
name = [' Alex ', ' Wupeiqi ', ' Yuanhao ', ' Nezha ']
def AD (L):
Return L[:len (L)]+ ' _SB '
Print (List (map (AD, name)))
****************************************************************************
4. Use the filter function to process the list of numbers and filter out all the even number in the list
num = [1,2,3,4,5,6,7,8]
Def ou (NUM1):
return NUM1% 2 = = 0
Print (List (filter (ou,num)))
****************************************************************************
5. Feel free to write a file with more than 20 lines
Run the program, read the contents into memory first, and store it in a list.
Receive user input page number, each page 5, output only when the content of the page
L=[]
f = open (' Test ', ' R ', encoding= ' utf-8 ')
L.append (F.read ())
A = L[0].split (' \ n ')
F.close ()
page = Int (input (' page>> '). Strip ())
Print (A[page*5-5:page*5])
For I in range (len (b)//5+1):
****************************************************************************
# 6. As follows, each dictionary name corresponds to the stock name, the shares corresponds to how many shares, price corresponds to the stock prices
Portfolio = [
{' name ': ' IBM ', ' shares ': +, ' price ': 91.1},
{' name ': ' AAPL ', ' shares ': +, ' price ': 543.22},
{' name ': ' FB ', ' shares ': $, ' Price ': 21.09},
{' name ': ' HPQ ', ' shares ': +, ' price ': 31.75},
{' name ': ' YHOO ', ' shares ': $, ' Price ': 16.35},
{' name ': ' ACME ', ' shares ': +, ' price ': 115.65}
]
Prices = [{' name ': item[' name '], ' prices ': item[' shares ']*item[' price ']} for item in portfolio]
Print (prices)
#
6.1. Calculate the total price of each stock purchased
L=[]
For I in range (len (portfolio)):
# Print (i)
Print (portfolio[i][' name '], ' Total price%s '% (portfolio[i][' shares ']*portfolio[i][' prices '))
L.append (portfolio[i][' price ')
Print (L)
# 6.2. Filter out the stock with a unit price greater than 100
def money (Qian):
Return qian > 100
Print (List (filter (money,l)))
Python diary----2017.8.1