- Suppose you have a list named sayings with each entry containing a famous quote. Which expression returns the shortest quote from the list?
min(sayings,key=lambda n:len(n))
- 排序:字典排序
a = sorted(dic, key = lambda x: dic[x])
map:一一对应
map(lambda x,y: x*y,[1,2,3,4],[3,2,1,0])
Out[2]: [3, 4, 3, 0]
temps_f = [74, 78, 82, 90, 89, 75, 74, 84, 82, 75]
map(lambda x: (5.0/9) * (x - 32),temps_f)
Out[3]:
[23.333333333333336,
25.555555555555557,
27.77777777777778,
32.22222222222222,
31.666666666666668,
23.88888888888889,
23.333333333333336,
28.88888888888889,
27.77777777777778,
23.88888888888889]
Filtering:筛选
nums = [63, 48, 60, 55, 67, 94, 63, 20, 99, 58]
filter(lambda x: x % 2 == 0,nums)
Out[4]: [48, 60, 94, 20, 58]
reduce: 依次
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5)