#大è¯é¢å°å½æ°(2) ä¸ä¸è®²åæ¬è®²çæ 颿¯â大è¯é¢å°å½æ°âï¼æè°å¤§è¯é¢ï¼å°±æ¯è¿äºå½æ°å¦ææº¯æºï¼é½ä¼æ¾å°å¬èµ·æ¥æ´é«å¤§ä¸çä¸è¥¿ãè¿ç§æç»´æ¹å¼ç»å¯¹æåå®å°ç»§æ¿äºä¸åæ°æçä¼è¯ä¼ ç»çãèªä»å¤©æçè£æ°çå°è±å½äººå¼å§è¸¢è¶³çï¼ä¸ç´å°ç°å¨æè°æå½åèµ·äºï¼é½ä¸ç´å¨è¯å¾è®ºè¯è¶³çèµ·æºäºè¯¥æçåååæçæå½æ¶ä»£ï¼å¹¶ä¸è¿æ¬åºäºé£æ¶åçä¸ä¸ªå«åé«ä¿ ççææ¥è®ºè¯ï¼å½ç¶äºï¼åèµ·çæå½æ¯æ¡ä¸ä½è¯¥å½å®¶éå¨ä¸çæ¯å¾ç¨ä¸çé³ç¿ï¼åªè½ç¨é«ä¿ æ¥ææ·«ä¸çªäºãè¿ç§æç»´æ¹å¼ï¼ææ¯åå®å°ç»§æ¿ï¼å ä¸ºå¨ææé¿è¿ç¨ä¸ï¼å®ä¸ç´è¢«å¥ä¸ºä¼è¯ä¼ ç»ãé¿Qæ¬æ¥æ¯å§èµµçï¼åèµµèç·æ¯æ¬å®¶ï¼æ¯ç§æè¦é¿ä¸è¾ï¼è½ç¶è¢«èµµèç·æäºå´ã åºè¯å°è¯´ï¼ä¹¦æ¥åæï¼å·²ç»ç ç©¶äºmapï¼ä¸é¢æ¥çreduceã å¿ä¸ä½è¿å¾æ¥ç¹åºè¯ãä¸ç¥éç宿¯ä¸æ¯å¬è¯´è¿MapReducï¼å¦ææ²¡æï¼é£ä¹Hadoopå¢ï¼å¦æè¿æ²¡æï¼å°±googleä¸ä¸ãä¸é¢æ¯æä»[ç»´åºç¾ç§](http://zh.wikipedia.org/wiki/MapReduce)䏿䏿¥çï¼å ±èµä¹ã >MapReduceæ¯Googleæåºçä¸ä¸ªè½¯ä»¶æ¶æï¼ç¨äºå¤§è§æ¨¡æ°æ®éï¼å¤§äº1TBï¼çå¹¶è¡è¿ç®ãæ¦å¿µâMapï¼æ å°ï¼âåâReduceï¼åç®ï¼âï¼åä»ä»¬çä¸»è¦ææ³ï¼é½æ¯ä»å½æ°å¼ç¼ç¨è¯è¨åæ¥çï¼è¿æä»ç¢éç¼ç¨è¯è¨åæ¥çç¹æ§ã ä¸ç¨ç®¡æ¯ä¸æ¯çæï¼æ»ä¹åå¯ä»¥ç¨å¼å¤´çææ³ææ·«ä¸ä¸äºï¼åæ¥ä»å¤©è¦é¼æ£çè¿ä¸ªreduceè¿è·å¤§æ°æ®æå ³åãä¸ç®¡æä¹æ ·ï¼ä½ ææ¢¦ä¸è¬çæè§å°±è¡ã ##reduce åå°ç°å®ï¼æ¸ éä¸ä¸ï¼ç»§ç»æ²ä»£ç ï¼ >>> reduce(lambda x,y: x+y,[1,2,3,4,5]) 15 请çå®ä»ç»è§å¯ï¼æ¯å¦è½å¤çåºæ¯å¦ä½è¿ç®çå¢ï¼ç»ä¸ä¸ªå¾ï¼  è¿è®°å¾mapæ¯æä¹è¿ç®çåï¼å¿äºï¼ç代ç ï¼ >>> list1 = [1,2,3,4,5,6,7,8,9] >>> list2 = [9,8,7,6,5,4,3,2,1] >>> map(lambda x,y: x+y, list1,list2) [10, 10, 10, 10, 10, 10, 10, 10, 10] çå®å¯¹æ¯ä¸ä¸ï¼å°±ç¥é两个çåºå«äºã忥mapæ¯ä¸ä¸è¿ç®ï¼reduceæ¯æ¨ªçé个å ç´ è¿è¡è¿ç®ã æå¨çè§£éæ¥èªå®ç½ï¼ >reduce(function, iterable[, initializer]) >Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to: def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: try: initializer = next(it) except StopIteration: raise TypeError('reduce() of empty sequence with no initial value') accum_value = initializer for x in it: accum_value = function(accum_value, x) return accum_value å¦æç¨æä»¬çæçforå¾ªç¯æ¥åä¸é¢reduceçäºæ ï¼å¯ä»¥è¿æ ·æ¥åï¼ >>> lst = range(1,6) >>> lst [1, 2, 3, 4, 5] >>> r = 0 >>> for i in range(len(lst)): ... r += lst[i] ... >>> r 15 foræ®ä¸çï¼reduceæ¯ç®æ´çã 为äºé»ç¼æç»´ï¼çè¿ä¹ä¸ä¸ªé®é¢ï¼æä¸¤ä¸ªlistï¼a = [3,9,8,5,2],b=[1,4,9,2,6],计ç®ï¼a[0]*b[0]+a[1]*b[1]+...çç»æã >>> a [3, 9, 8, 5, 2] >>> b [1, 4, 9, 2, 6] >>> zip(a,b) #å¤ä¹ ä¸ä¸zipï¼ä¸é¢çæ¹æ³ä¸è¦ç¨å° [(3, 1), (9, 4), (8, 9), (5, 2), (2, 6)] >>> sum(x*y for x,y in zip(a,b)) #è§£æåç´æ¥æ±å 133 >>> new_list = [x*y for x,y in zip(a,b)] #å¯ä»¥ç忝ä¸é¢æ¹æ³çåå¸å®æ½ >>> #è¿æ ·è§£æä¹å¯ä»¥ï¼new_tuple = (x*y for x,y in zip(a,b)) >>> new_list [3, 36, 72, 10, 12] >>> sum(new_list) #æè :sum(new_tuple) 133 >>> reduce(lambda sum,(x,y): sum+x*y,zip(a,b),0) #è¿ä¸ªæ¹æ³æ¯å¨èé ·å¢åï¼ 133 >>> from operator import add,mul #èé ·çæ¹æ³ä¹ä¸æ¢ä¸ä¸ª >>> reduce(add,map(mul,a,b)) 133 >>> reduce(lambda x,y: x+y, map(lambda x,y: x*y, a,b)) #map,reduce,lambdaé½é½å ¨äºï¼æ´é ·åï¼ 133 ##filter filterç䏿å«ä¹æ¯âè¿æ»¤å¨âï¼å¨pythonä¸ï¼å®å°±æ¯èµ·å°äºè¿æ»¤å¨çä½ç¨ãé¦å ç宿¹è¯´æï¼ >filter(function, iterable) >Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed. >Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None. è¿æ¬¡ççä¸ç¿»è¯äºï¼å¥½å以å¾ä¹æ²¡ææä¹ç¿»è¯åï¼ï¼èä¸ä¹ä¸è§£éè¦ç¹äºã请åä½å¡å¿ èªå·±é 读ä¸é¢çæåï¼å¹¶ä¸çè§£å ¶å«ä¹ãè±è¯ï¼æ 论æä¹å¼ºè°é½æ¯ä¸è¿åçï¼åªææ¯åä¹ä¸ï¼è¯´ä¸¤å¥è±è¯ï¼æ²¡åè¿å¯ä»¥è®¨å°è±éç¾å å¢ã éè¿ä¸é¢ä»£ç ä½ä¼ï¼ >>> numbers = range(-5,5) >>> numbers [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4] >>> filter(lambda x: x>0, numbers) [1, 2, 3, 4] >>> [x for x in numbers if x>0] #ä¸ä¸é¢é£å¥çæ [1, 2, 3, 4] >>> filter(lambda c: c!='i', 'qiwsir') #è½ä¸è½å¯¹åºä¸é¢ææ¡£è¯´æé£å¥è¯å¢ï¼ 'qwsr' #âIf iterable is a string or a tuple, the result also has that type;â è³æ¤ï¼ç¨ä¸¤æ¤ä»ç»äºå 个å°å½æ°ï¼è¿äºå½æ°å¨å¯¹ç¨åºçæ§è½æé«ä¸ï¼å¹¶æ²¡ææ¾èæè 稳å®é¢æï¼ä½æ¯ï¼å¨ä»£ç çç®æ´ä¸ï¼æ¯æç®å ±ç¹çãææ¶åæ¯å¯ä»¥ç¨æ¥ç§ä¸ç§ï¼å½°æ¾pythonçä¼é åèªå·±èé ·ã