----------------------Homework One
# There are two lists for the old boys to enroll in the students ' names for Linux and Python courses
# linux=[' Steel Bullets ', ' small Gecko ', ' Little Tiger ', ' Alex ', ' Wupeiqi ', ' Yuanhao '
# python=[' dragon ', ' Steel bullets ', ' zhejiangF4 ', ' Little Tiger '
#
# Question one: Get a list of students who sign up for both Linux and Python
# Question two: Get a list of students who only enroll in Linux and don't sign up for Python
# Question three: Get a list of students who only sign up for Python and not enroll in Linux
linux=[' steel bullets ', ' small Gecko ', ' Little Tiger ', ' Alex ', ' Wupeiqi ', ' Yuanhao '
python=[' dragon ', ' Steel bullets ', ' zhejiangF4 ', ' Little Tiger '
res1=[i for I in Linux for J in Python if I==j]
print (RES1)
res2=[i for I in Linux if I is not in Python]
print (res2)
res3=[i for i in Python if I is not in Linux]
print (RES3)
-----------------------Job Two
shares = {
' IBM ': 36.6,
' Lenovo ': 27.3,
' Huawei ': 40.3,
' Oldboy ': 3.2,
' Ocean ': 20.1
}
#
# Question one: A list of stock names with a stock price greater than 30
# Question two: Find out the total price of all stocks
res = [k for k,v in Shares.items () if V>30]
# Print (res)
Total=sum ([V for K,v in Shares.items ()])
print (total)
------------------------Job Three
l=[10,2,3,4,5,6,7]
# Get a new list L1, each element in the new list is the square of the value of each element in L
# filter out values greater than 40 in L1, then sum
l1=[(x*x) for x in L]
print (L1)
res= (i-I in L1 if float (i) >40)
Print (res)
Total=sum (k for k in Res)
print (total)
#print (List ((x*x) for x in L))
---------------------------Job Four
#用map来处理字符串列表啊, turn everyone in the list into SB, for example ALEX_SB
# name=[' Alex ', ' Wupeiqi ', ' Yuanhao '
#用map来处理下述l and then use list to get a new list in which everyone's name is SB end
# >>> l=[{' name ': ' Alex '},{' name ': ' Y '}]
name=[' Alex ', ' Wupeiqi ', ' Yuanhao ']
Name1=map (lambda name:name+ "_SB", name)
For i in name1:
print (i)
l=[{' name ': ' Alex '},{' name ': ' Y '}]
M=map (lambda k:k["name"]+ "SB", L)
Print (list (m))
------------------------Job Five
#用filter来处理, get stock names with stock prices greater than 20
shares={
' IBM ': 36.6,
' Lenovo ': 23.2,
' Oldboy ': 21.2,
' ocean ': 10.2,
}
F=filter (lambda d:shares[d]>20,shares)
Print (list (f))
print (f)
For I in F:
print (i)
-----------------------------Job Six
#如下, the name of each dictionary corresponds to the stock name, shares corresponds to how many shares, price corresponds to the prices of the stock
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}
]
# 1:map comes up with a number-containing iterator, which refers to the total price of each stock purchased
M=map (lambda d:d["shares"]*d["Price"],portfolio)
For I in M:
print (i)
# 2: Based on 1 results, use reduce to calculate how much money is spent on buying these shares
From functools import reduce
total=reduce (lambda x,y:x+y,m)
print (total)
# 3: What are the stocks with filter with a unit price greater than 100
F=filter (Lambda d:d["Price"]>100,portfolio)
print (f)
For I in F:
print (i)
python\ function Job