map()
The function receives two parameters, one is a function, the other is a sequence, map
Functions the passed-in function sequentially to each element of the sequence and returns the result as a new list.
defNum(x):
ReturnX*X
PrintMap(num,[1,2,3,4,5,6,7,8,9])
# [1, 4, 9, +, A, $, +,--]
reduce()
A function that acts on a sequence [X1, x2, x3 ...] , the function must receive two parameters, and reduce calculates the result and the next element of the sequence, and the effect is:
def add (x, y) :
return x+ y
print reduce (Add,[1 , 3 , 5 , 7 , 9 ])
#25
defList2num(x, Y):
ReturnX*Ten+Y
PrintReduce(list2num,[1,3,5,7,9])
#13579
sorted()
The function can sort the list:
PrintSorted([3,6,8,9,4,1,2])
#[1, 2, 3, 4, 6, 8, 9]
defreversed_cmp(x, Y):
IfX>Y:
Return-1
Else:
Return1
return0
PrintSorted([3,6,8,9,4,1,2],REVERSED_CMP)
# [9, 8, 6, 4, 3, 2, 1]
The python closures are similar to those in javascript:
defLazy_sum(*Args:
defs():
Returnsum([x forXinchArgs])
returnS
F= Lazy_sum (1,2,3,4,5)
PrintF ()
# 15
F2= Lazy_sum ()
PrintF2 ()
# 0
Implementing anonymous functions via lambda:
F = lambda x: x*x
print F (5)
# 25
10 minutes a day, 12 days Getting Started Python (four)