movie_people=["sb+_alex","sb_wupeiqi","han"] # def filter_test(array): # ret=[] # for p in array: # if not p.startswith('sb'): # ret.append(p) # # return ret # # end=filter_test(movie_people) # print(end) # movie_people=["alex","sb_wupeiqi","han_sb"] # def sb_show(n): # return n.endswith('sb') # #filter_test DEF (FUNC, Array): # RET = [] # for P in Array: # IF Not FUNC (P): # ret.append (P) # # return RET # # End = filter_test (sb_show, movie_people) # Print (End) # ultimate version # the lambda the n-: n.startwith ( 'SB') DEF filter_test (FUNC, Array): # Note that in the function call, try not to direct incoming global variables, which will modify the values of global variables as much as possible use parameter assignment RET = [] for P in Array: IF Not FUNC (P): ret.append (P) return ret res=filter_test(lambda n:n.startswith('sb'),movie_people) print(res) #filter函数 print(list(filter(lambda n: not n.startswith('sb'),movie_people)))
num_1=[1,2,3,4,5,6,100] res=0 for num in num_1: res+=num print(res) # def multi(x,y): # return x*y #lambda:x,y:x*y #num_l=[1,2,3,100] # def reduce_test(func,array): # res=array[0] # for num in array: # res=func(res,num) # return res # print(reduce_test(lambda x,y:x*y,num_l)) num_l=[1,2,3,100] def reduce_test(func,array,init=None): # code to determine whether there is often a variable None, there are three main wording: # The first one is the # `the X-IF IS None # `; # second is # `IF not the X-:`; # The third is # `iF X iS none Not # ` (more clearly understood phrase # `Not iF (X iS none) # `) iF Not the init: # the init whether none RES = Array.pop (0) the else : RES = the init for NUM in Array: RES = FUNC (RES, NUM) returnRES Print (reduce_test ( the lambda X, Y: X * Y, num_l, 100 )) from functools Import the reduce # the reduce function: Merge the final result sequence derived Print (the reduce ( the lambda X, Y: X * Y, num_l, 100) )
# Processing each element of the sequence, the result is a 'list', the 'list' and the number of elements in the same position as the original
# map ()
#fileter through each element of the sequence, each element of the Boolean determined value, if True is left behind to give the result is a list of
people = [{ "name": "alex", "age": 10000}, { "name": "han", "age": 1000}, { "name": "OU", "Age": 18 is}]
Print (List (filter (the lambda P: P [ 'Age'] <= 18 is, people)))
#reduce: a processing sequence, and the sequences merged operation
from the reduce functools Import
Print (the reduce (the lambda X, Y: X + Y, Range (100), 100)) # 3 initial parameter values