{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# operator, functools, itertools, toolz, fn, funcy 模块" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## operator 模块" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import operator as op" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`operator` 模块提供了各种操作符(`+,*,[]`)的函数版本方便使用:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "加法:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "45\n" ] } ], "source": [ "print reduce(op.add, range(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "乘法:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "362880\n" ] } ], "source": [ "print reduce(op.mul, range(1,10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`[]`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('a', 1), ('bb', 4), ('ccc', 2), ('dddd', 3)]\n", "[('a', 1), ('ccc', 2), ('dddd', 3), ('bb', 4)]\n", "[('a', 1), ('bb', 4), ('ccc', 2), ('dddd', 3)]\n" ] } ], "source": [ "my_list = [('a', 1), ('bb', 4), ('ccc', 2), ('dddd', 3)]\n", "\n", "# 标准排序\n", "print sorted(my_list)\n", "\n", "# 使用元素的第二个元素排序\n", "print sorted(my_list, key=op.itemgetter(1))\n", "\n", "# 使用第一个元素的长度进行排序:\n", "print sorted(my_list, key=lambda x: len(x[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## functools 模块" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`functools` 包含很多跟函数相关的工具,比如之前看到的 `wraps` 函数,不过最常用的是 `partial` 函数,这个函数允许我们使用一个函数中生成一个新函数,这个函数使用原来的函数,不过某些参数被指定了:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "24\n" ] } ], "source": [ "from functools import partial\n", "\n", "# 将 reduce 的第一个参数指定为加法,得到的是类似求和的函数\n", "sum_ = partial(reduce, op.add)\n", "\n", "# 将 reduce 的第一个参数指定为乘法,得到的是类似求连乘的函数\n", "prod_ = partial(reduce, op.mul)\n", "\n", "print sum_([1,2,3,4])\n", "print prod_([1,2,3,4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`partial` 函数还可以按照键值对传入固定参数。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## itertools 模块" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`itertools` 包含很多与迭代器对象相关的工具,其中比较常用的是排列组合生成器 `permutations` 和 `combinations`,还有在数据分析中常用的 `groupby` 生成器:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from itertools import cycle, groupby, islice, permutations, combinations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`cycle` 返回一个无限的迭代器,按照顺序重复输出输入迭代器中的内容,`islice` 则返回一个迭代器中的一段内容:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b']\n" ] } ], "source": [ "print list(islice(cycle('abcd'), 0, 10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`groupby` 返回一个字典,按照指定的 `key` 对一组数据进行分组,字典的键是 `key`,值是一个迭代器: " ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 ['pig', 'cow', 'dog', 'cat']\n", "4 ['lion']\n", "5 ['hippo', 'tiger']\n", "7 ['giraffe']\n", "8 ['elephant']\n", "\n" ] } ], "source": [ "animals = sorted(['pig', 'cow', 'giraffe', 'elephant',\n", " 'dog', 'cat', 'hippo', 'lion', 'tiger'], key=len)\n", "\n", "# 按照长度进行分组\n", "for k, g in groupby(animals, key=len):\n", " print k, list(g)\n", "print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "排列:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['abc', 'acb', 'bac', 'bca', 'cab', 'cba']\n" ] } ], "source": [ "print [''.join(p) for p in permutations('abc')]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "组合:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]\n" ] } ], "source": [ "print [list(c) for c in combinations([1,2,3,4], r=2)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## toolz, fn 和 funcy 模块" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这三个模块的作用是方便我们在编程的时候使用函数式编程的风格。" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }