posts - 431,  comments - 344,  trackbacks - 0

          原文:http://www.liuzhongshu.com/code/python-map-filter-reduce.html
          python有幾個內(nèi)置的函數(shù)很有意思:map/filter/reduce,都是對一個集合進行處理,filter很容易理解用于過濾,map用于映射,reduce用于歸并,這幾個詞還是太抽象了,下面的代碼精確的說明了這幾個函數(shù)的用途:

          def map_imp(function, sequence) :
              if function is None: return list(sequence)
              retvals = []
              for element in sequence:
                  if (function(element)):
                      retvals.append(element)
                      return retvals
          def reduce_imp(function, sequence) :
              arg1 = function(sequence[0])
              for arg2 in sequence[1:] :
                  arg1 = function(arg1, arg2)
              return arg1
          def filter_imp(function, sequence) :
              retvals = []
              for element in sequence:
                  if (function is None and element) or function(element) :
                      retvals.append(element)
              return retvals

          對于這種邏輯性強的東西,用語言來描述是無助的,代碼則要清晰的多。

          具體使用方法可以參考另外一篇:http://www.aygfsteel.com/rain1102/archive/2009/06/01/279478.html

          posted on 2009-06-14 22:08 周銳 閱讀(927) 評論(0)  編輯  收藏 所屬分類: Python
          主站蜘蛛池模板: 沈丘县| 炎陵县| 兴仁县| 达拉特旗| 商河县| 临城县| 信阳市| 湟源县| 邢台县| 大安市| 丹巴县| 宁明县| 巴青县| 道孚县| 鱼台县| 漳平市| 新巴尔虎左旗| 东方市| 永丰县| 科尔| 漯河市| 珠海市| 通化县| 息烽县| 崇明县| 鹤庆县| 喀喇沁旗| 西乌| 陈巴尔虎旗| 印江| 克山县| 湛江市| 福贡县| SHOW| 宁国市| 启东市| 天峨县| 大姚县| 白玉县| 镇康县| 淳化县|