---restore content starts---
1.map
1) map is actually quite right. The operator does an abstraction and returns an object, but here is no idea why you can't print two times for a map return variable, because it's recycled?
def f (x): return x*xtmp = Map (F,range (6)) Tmps = Map (Str,range (6)) Print (List (TMP)) #print (list (tmps)) print (Type ( Range (6)) #range返回的就是range类型 <class ' range ' >print (set (Tmps)) Print (list (tmps)) #此时的输出不在是range6的List而是一个空的 []
2) reduce requires more than two parameters to be used, typically a list on the scope, such as the sum below
1 tadd = reduce (Add,range (6))2print (tadd)3 for inif x%2! = 0])4print (tadd)
View Code
reduceTo function on a sequence [x1, x2, x3, ...] , the function must receive two parameters, reduce and the result continues and the next element of the sequence is accumulated, the effect is:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
1 #-*-coding:utf-8-*-2 """3 Spyder Editor4 5 This is a temporary script file.6 """7 fromFunctoolsImportReduce8Tadd = Reduce (Add,range (6))9 Print(Tadd)TenTadd = reduce (add, [x forXinchRange (6)ifx%2! =0]) One Print(Tadd) ATadd = reduce (fn,[x forXinchRange (9)ifx%2! =0]) - Print(Tadd)
View Code
3) for some versions of simplification:
1 fromFunctoolsImportReduce2 deff (x):3 returnx*x4 5 defAdd (x1,x2):6 returnx1+X27 deffn (x1,x2):8 return10*x1+X29 Ten defChar2num (s): One return{'0'70A'1': 1,'2': 2,'3': 3,'4': 4,'5'75A'6': 6,'7': 7,'8': 8,'9': 9}[s] A - defStr2Int (s): - defChar2num (s): the return{'0'70A'1': 1,'2': 2,'3': 3,'4': 4,'5'75A'6': 6,'7': 7,'8': 8,'9': 9}[s] - - deffn (x1,x2): - return10*x1+X2 + returnReduce (Fn,map (char2num,s)) - defStr2int_2 (s): + returnReduceLambdax,y:x*10+Y,map (char2num,s)) A atTMP = Map (F,range (6)) -Tmps = Map (Str,range (6)) - Print(List (TMP)) - #Print (list (tmps)) - Print(Type (range (6)))#Range returns the range Type <class ' range ' > - Print(Set (tmps)) in Print(List (tmps))#the output at this time is not a list of range6, but an empty one [] - toTadd = Reduce (Add,range (6)) + Print(Tadd) -Tadd = reduce (add, [x forXinchRange (6)ifx%2! =0]) the Print(Tadd) *Tadd = reduce (fn,[x forXinchRange (9)ifx%2! =0]) $ Print(Tadd)Panax Notoginseng Print(Reduce (FN, map (Char2num,'213')))) - Print(Str2Int ('1231')) the Print(Str2int_2 ('321'))View Code
---restore content ends---
Python Learning Note map&&reduce