美文网首页
【Python】-014-函数-函数式编程-2

【Python】-014-函数-函数式编程-2

作者: 9756a8680596 | 来源:发表于2017-08-08 13:47 被阅读7次

python内置高阶函数

  • Filter函数
    • filter(function, sequence) -> list, tuple, or string,会对指定序列执行过滤操作。

    • 对字符串/列表/元组等sequence类型进行过滤,sequence中的item依次执行function(item)

    • 将执行结果为Trueitem组成一个List/String/Tuple(取决于sequence的类型)返回

      def odds(x):
        return x%2==0
      print filter(odds, range(1, 101))  //返回列表,包含1~100的偶数,1%2 == False,不计入,2%2==0 True,计入...
      
  • Map函数
    • map(function, iterable[, iterable, ...]) -> list,根据提供的函数对指定序列做映射

    • iterable中的item依次执行function(item)映射,执行结果输出为list

      def  mul3(a):
        return a*3
      L = range(0, 11)
      print 'list is ', L  //L is  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],key=1,2,3...10
      print 'L mapping ', map(mul3, L)  //L mapping  [3, 6, 9, 12, 15, 18, 21, 24, 27, 30],value=1*3, 2*3, ...
      
  • Reduce函数
    • reduce(function, sequence[, initial]) -> value,对参数序列sequence中元素进行累积

    • 依次从sequence中取一个元素,和上一次调用function的结果做参数再次调用function

      def add(x, y):
        return x+y
      print reduce(add, range(1, 11), 0)  //0+1, 1+2, 3+4,...55, f(f(f(f(a, b), c), d)...)
      

统计文章中哪些词汇出现的频率最高

  • 统计文章中,单词出现的次数
    • 需要注意单词的格式化,去除特殊符号和单词大小写对相同单词的影响
    • 过程中的数据可用列表/元组方法操作,最后转换为字典即可
  • 重复操作多篇文章
    • 应用高阶函数map(function, iterable[, iterable, ...]) -> list完成关系映射,生成每篇文章的词频统计数据
  • 将所有文章统计后汇总
    • 应用高阶函数reduce(function, sequence[, initial]) -> value完成累积操作,对每篇文章的词频进行汇总,相同项求和,不同项加入总的词频数据

      # coding: utf-8
      def readFile(filename):  //统计一篇文章中的单词和词频,返回二者构成的元组列表
          f = open(filename, 'r')
          washlist = []  //单词格式化后的列表
          savedlist = []  //已经存入词频列表的单词
          freqlist = []  //词频列表
      
          for line in f:
              wordlist = line.lower().strip().split()  //格式化,去除头尾空白字符,并分割为待清洗的单词列表
              for word in wordlist:
                  lastchar = word[-1]  //待清洗单词列表的每个单词的词尾
                  firstchar = word[0]  //待清洗单词列表的每个单词的词头
                  if lastchar in ["!", ".", ";", "\"", "\'"]:  //判断词尾是否有特殊字符
                      word = word.strip(lastchar)
                  elif firstchar in ["!", ".", ";", "\"", "\'"]:  //判断词头是否有特殊字符
                      word = word.strip(firstchar)
                  washlist.append(word)  //清洗了特殊字符的单词列表
          for word in washlist:  //统计清洗后列表中的词频,用到列表的count方法
              if not word in wordsaved:
                  count = washlist.count(word)
                  wordsaved.append(word)
                  freqlist.append((word, count))  //将单词和词频的元组构成列表
      
          f.close()
          return freqlist
      
      def merge(listA, listB):
          mergelist = []
          wordA, countA = zip(*listA)  //返回列表A的单词和词频分别构成的元组
          wordB, countB = zip(*listB)  //返回列表B的单词和词频分别构成的元组
      
          for word, count in listB:  //遍历有单词和词频元组构成的列表B
              if word in wordA:  //如果单词在列表A中存在,合并二者的词频数据,将单词和新的词频放入合并后的元组列表mergelist中
                  count = countA[wordA.index(word)] + count
              mergelist.append((word, count))
      
          for word, count in listA:  //再讲只存在列表A的单词和词频的元组放入mergelist中
              if not word in wordB:
                  mergelist.append((word, count))
      
          return mergelist
      
      if __name__ == '__main__':
          files = ["article_000.txt", "article_001.txt", "article_002.txt", "article_003.txt", "article_004.txt", "article_005.txt"]
          freqlist = map(readFile, files)  ##return the (word,count) list for each articals
          totallist = reduce(merge, freqlist) ##merge each wordlist of artical
      

参考资料
1.从两个例子看 Python【map、reduce、filter】内置函数的使用
2.[Python] 函数lambda(), filter(), map(), reduce()
3.python中的map、filter、reduce函数

相关文章

  • 【Python】-014-函数-函数式编程-2

    python内置高阶函数 Filter函数filter(function, sequence) -> list, ...

  • Python函数式编程指南

    注:采转归档,自己学习查询使用 Python函数式编程指南(1):概述Python函数式编程指南(2):函数Pyt...

  • 高阶python 函数式编程

    高阶python 函数式编程 - 函数式 函数式编程(FunctionalProgramming) - 基于lam...

  • Python进阶笔记

    文|Seraph 函数式编程 1 纯函数式编程:不需要变量、没有副作用、测试简单2 Python不是纯函数式编程(...

  • 向量化

    python向量化本身做得不是很好需要借助函数式编程或者列表推导式实现 1 列表推导式 2 函数式编程

  • Python进阶语法——函数式编程、模块,面向对象

    一、 Python进阶学习 一、函数式编程 1.1函数式编程 1.2高阶函数 1.2.1 import mathd...

  • Python高阶函数

    本篇将介绍Python的函数式编程,介绍高阶函数的原理,更多内容请参考:Python学习指南 函数式编程 函数是P...

  • Python函数式编程

    虽然 Python 不是函数式编程语言(是命令式编程语言),但是支持许多有价值的函数式编程工具。Python 提供...

  • 【第十五天】函数式与并行运算

    第七章 函数式编程 7.1 1.Python中的函数式 函数式编程强调了函数的纯粹性(purity)一个纯函数是没...

  • python必知必会7

    Python 支持函数式编程吗? 近些年来,由于函数式编程易于调试和测试的优点,函数式编程越来越受到关注。虽然 P...

网友评论

      本文标题:【Python】-014-函数-函数式编程-2

      本文链接:https://www.haomeiwen.com/subject/oijylxtx.html