#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#lambda(入口參數)(返回的值的表達式)(條件)print filter(lambda x:x>3,[1,2,3,4,5])
print"---------------------------------"
print (lambda x,y: x if x> y else y)(101,102)
print"---------------------------------"
print filter(lambda x:x%3==0,[1,2,3,4,5,6])
print"---------------------------------"
#函數列表
L = [lambda x:x**2,lambda x:x**3,lambda x:x**4]
for f in L:print f(2)
print"---------------------------------"
print L[0](3)
#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print"-----------------------01------------------------"
x = (lambda x="Boo",y="Too",z="Zoo": x+y+z)
print x("Foo")
print"-----------------------02------------------------"L = [lambda x:x**2,lambda x:x**3,lambda x:x**4]
for f in L:print f(2)
print L[0](3)
print"---------------------03--------------------------"key = 'B'
dic = { 'A':lambda: 2*2,'B':lambda: 2*4,'C':lambda: 2*8}
print dic[key]()print"----------------04-------------------------------"lower = lambda x,y: x if x<y else y
print"lower('aa','ab')"print"------------------05有點問題-----------------------------"showall = lambda x:list(map(sys.stdout.write,x))
print type(showall(['Jerry\n','Sherry\n','Alice\n']))
print"------------------05-----------------------------"
print showall(['Jerry','Sherry','Alice'])
print"------------------05-----------------------------"
showall = lambda x: [sys.stdout.write(line) for line in x]
print showall(('I\t','Love\t','You!'))print"------------------06-----------------------------"from Tkinter import Button,mainloop
x = Button(text='Press me',command=(lambda:sys.stdout.write('Hello,World\n')))
x.pack()
x.mainloop()
print"------------------07-----------------------------"
out = lambda *x: sys.stdout.write(' '.join(map(str,x)))
print out('This','is','a','book!\n')
print"------------------08-----------------------------"
print (lambda x: x.startswith('B'))('Bob')
Names = ['Anne', 'Amy', 'Bob', 'David', 'Carrie', 'Barbara', 'Zach']
B_Name= filter(lambda x: x.startswith('B'),Names)
print B_Name
print"------------------09----------------------------"
squares = map(lambda x:x**2,range(5))#參數,返回結果,范圍
print squaresprint"-----------------10------------------------------"
squares = map(lambda x:x**2,range(10))#先獲得0-9的平方數
print squares
filters = filter(lambda x:x>5 and x<50,squares)#從這些平方數中挑選出大于5并且小于50的
print filtersprint"-----------------11------------------------------"death = [('James',32),('Alies',20),('Wendy',25)]
print sorted(death,key=lambda age:age[0])
print sorted(death,key=lambda age:age[1]) print"-----------------12------------------------------"
L = [1,2,3,4]
sum = reduce(lambda x,y:x+y,L)
print sum
print"-----------------13------------------------------"nums = range(2,50)
for i in nums:nums = filter(lambda x:x==i or x % i,nums)
print nums
print"-----------------14------------------------------"
a = [1,2,3,4]
b = [5,6,7,8]
print map(lambda x,y:x+y, a,b)#a賦值給x,b賦值給yprint"-----------------15------------------------------"
sentence = "Welcome To Beijing!"
words = sentence.split()
print "words=",words
lengths = map(lambda x:len(x),words)#把words傳給x
print lengths
其中reducede用法如下:
reduce函數
reduce函數,reduce函數會對參數序列中元素進行累積。
reduce函數的定義:
reduce(function, sequence[, initial]) -> value
function參數是一個有兩個參數的函數,reduce依次從sequence中取一個元素,和上一次調用function的結果做參數再次調用function。
第一次調用function時,如果提供initial參數,會以sequence中的第一個元素和initial作為參數調用function,否則會以序列sequence中的前兩個元素做參數調用function。
reduce(lambda x, y: x + y, [2, 3, 4, 5, 6], 1)
結果為21( ?(((((1+2)+3)+4)+5)+6) ?)
reduce(lambda x, y: x + y, [2, 3, 4, 5, 6])
結果為20
總結
以上是生活随笔為你收集整理的lambda的用法与实例(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。