|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在大部分时候,在一个算法中,需要重复执行一组语句,如果每次都重复写出来,不仅乏味而且编程效率比较低,降低程序的可读性。为了将重复的执行抽象出来,可使用函数将一组操作封装成一个整体,给一个名称和参数列表作为可变量的输入。Python中函数的定义为:"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "```\n",
- "def funcname(arg1, arg2,... argN):\n",
- " \n",
- " ''' Document String'''\n",
- "\n",
- " statements\n",
- "\n",
- "\n",
- " return <value>\n",
- "```"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "将上面的语法理解为,定义了一个名为`funcname`的函数,它接受`arg1,arg2,…argN`的参数。函数在执行语句后返回一个`<value>`。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hey Jack!\n",
- "Jack, How do you do?\n"
- ]
- }
- ],
- "source": [
- "print(\"Hey Jack!\")\n",
- "print(\"Jack, How do you do?\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "不需要每次都写上面的两个语句,可以通过定义一个函数来替换它,这个函数只需一行就能完成任务。\n",
- "\n",
- "定义一个函数 `first_func()`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def first_func():\n",
- " print(\"Hey Jack!\")\n",
- " print(\"Jack, How do you do?\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hey Jack!\n",
- "Jack, How do you do?\n",
- "Hey Jack!\n",
- "Jack, How do you do?\n"
- ]
- }
- ],
- "source": [
- "first_func()\n",
- "funca=first_func\n",
- "funca()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**first_func()** 每一次只打印一个人的消息。我们可以让我们的函数 **first_func()** 接受参数,该参数将存储名称然后打印相应地接受字符串。为了这样做我们需要像所示的那样在函数内添加一个参数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def first_func(username):\n",
- " print(\"Hey\", username + '!')\n",
- " print(username + ',' ,\"How do you do?\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Please enter your name : Python\n"
- ]
- }
- ],
- "source": [
- "name1 = input('Please enter your name : ')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "名称“Willam”实际上存储在name1中。我们将这个变量传递给函数**firstfunc()** 作为变量username,因为它是为这个函数定义的变量。即name1作为用户名传递。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hey Python!\n",
- "Python, How do you do?\n"
- ]
- }
- ],
- "source": [
- "first_func(name1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "让我们通过定义另一个函数**second_func()** 来进一步简化它,该函数接受名称并将其存储在一个变量中,然后从函数本身内部调用**first_func()**。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def first_func(username):\n",
- " print(\"Hey\", username + '!')\n",
- " print(username + ',' ,\"How do you do?\")\n",
- "def second_func():\n",
- " name = input(\"Please enter your name : \")\n",
- " first_func(name)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Please enter your name : Tom\n",
- "Hey Tom!\n",
- "Tom, How do you do?\n"
- ]
- }
- ],
- "source": [
- "second_func()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 1. 返回语句"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "当函数产生某个值,并且该值必须存储在一个变量中,或者需要返回或返回给主算法进行进一步操作时,使用return语句。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def times(x,y):\n",
- " z = x*y\n",
- " return z"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "上面定义的**times()** 函数接受两个参数并返回变量z,该变量包含两个参数的乘积结果。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "20\n"
- ]
- }
- ],
- "source": [
- "c = times(4,5)\n",
- "print(c)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "z值存储在变量c中,可以用于进一步的操作。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "可以在return语句中使用整个语句本身,而不是声明另一个变量,如下所示。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def times(x,y):\n",
- " '''This multiplies the two input arguments'''\n",
- " return x*y"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "20\n"
- ]
- }
- ],
- "source": [
- "c = times(4,5)\n",
- "print(c)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "由于现在定义了**times()**,我们可以如上所示记录它。在**help()** 函数下调用**times()** 函数时返回该文档。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Help on function times in module __main__:\n",
- "\n",
- "times(x, y)\n",
- " This multiplies the two input arguments\n",
- "\n"
- ]
- }
- ],
- "source": [
- "help(times)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "times?"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "也可以返回多个变量,但请记住顺序。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "eglist = [10,50,30,12,6,8,100]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def egfunc(eglist):\n",
- " highest = max(eglist)\n",
- " lowest = min(eglist)\n",
- " first = eglist[0]\n",
- " last = eglist[-1]\n",
- " return highest,lowest,first,last"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果调用函数时没有为其分配任何变量,则结果将在一个元组中返回。但是如果变量被提到了,那么结果会以特定的顺序分配给变量,这个顺序在return语句中声明。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(100, 6, 10, 100)\n"
- ]
- }
- ],
- "source": [
- "a = egfunc(eglist)\n",
- "print(a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " a = 100 \n",
- " b = 6 \n",
- " c = 10 \n",
- " d = 100\n"
- ]
- }
- ],
- "source": [
- "a,b,c,d = egfunc(eglist)\n",
- "print(' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 2. 隐式参数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "当一个函数的参数在大多数情况下是常见的或者它是`隐式的`时,使用这个概念。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def implicit_add(x, addnumber=3):\n",
- " return x+addnumber"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**implicit_add( )** 是一个函数接受两个参数,但大多数时候第一个参数只需要加3。因此,第二个参数被赋值为3。这里第二个参数是隐式的。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在,如果在调用**implicit_add()** 函数时没有定义第二个参数,则将其视为3。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "7"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicit_add(4)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "但如果指定了第二个参数,则此值将覆盖分配给该参数的隐式值"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "8"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicit_add(4,4)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "11"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicit_add(5, addnumber=6)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 3. 任意数量的参数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果函数要接受的参数数量未知,则在参数前使用星号。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def add_n(*args):\n",
- " res = 0\n",
- " reslist = []\n",
- " for i in args:\n",
- " reslist.append(i)\n",
- " print(reslist)\n",
- " return sum(reslist)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "上面的函数接受任意数量的参数,定义一个列表,并将所有参数附加到该列表中,并返回所有参数的总和。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4, 5]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "15"
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "add_n(1,2,3,4,5)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "6"
- ]
- },
- "execution_count": 26,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "add_n(1,2,3)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "参数列表也可以通过 \"param_name = value\" 的形式传入到函数"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[30, 10, 20]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "60"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "def add_nd(**kwargs):\n",
- " res = 0\n",
- " reslist = []\n",
- " for (k,v) in kwargs.items():\n",
- " reslist.append(v)\n",
- " print(reslist)\n",
- " return sum(reslist)\n",
- "\n",
- "add_nd(x=10, y=20, c=30)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 4. 全局和局部变量"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在函数内部声明的变量是局部变量,生命周期限于函数执行期间;在函数外部声明的是全局变量。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "eg1 = [1,2,3,4,5]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在下面的函数中,我们将在函数内部声明的列表中追加一个元素。函数内部声明的eg2变量是一个局部变量。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def egfunc1():\n",
- " eg1 = [1, 2, 3, 4, 5, 7]\n",
- " print(\"egfunc1>> eg1: \", eg1)\n",
- "\n",
- "def egfunc2():\n",
- " eg1.append(8)\n",
- " print(\"egfunc2>> eg1: \", eg1)\n",
- "\n",
- "def egfunc3():\n",
- " global eg1\n",
- " eg1 = [5, 4, 3, 2, 1]\n",
- " print(\"egfunc3>> eg1: \", eg1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "egfunc1>> eg1: [1, 2, 3, 4, 5, 7]\n",
- "eg1: [1, 2, 3, 4, 5] \n",
- "\n",
- "egfunc2>> eg1: [1, 2, 3, 4, 5, 8]\n",
- "eg1: [1, 2, 3, 4, 5, 8] \n",
- "\n",
- "egfunc3>> eg1: [5, 4, 3, 2, 1]\n",
- "eg1: [5, 4, 3, 2, 1] \n",
- "\n"
- ]
- }
- ],
- "source": [
- "egfunc1()\n",
- "print(\"eg1: \", eg1, \"\\n\")\n",
- "\n",
- "egfunc2()\n",
- "print(\"eg1: \", eg1, \"\\n\")\n",
- "\n",
- "egfunc3()\n",
- "print(\"eg1: \", eg1, \"\\n\")\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 5. Lambda 函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "程序中有时需要临时使用一个简单的函数,单独定义出来比较费事,为了提高编程效率,Python等很多语言引入了`Lambda`函数,这些Lambda函数没有使用任何名称定义,只携带一个表达式,返回的是函数本身(类似函数指针或者函数对象)。Lambda函数在操作列表时非常方便。这些函数由关键字**lambda**定义,后面跟着变量、冒号和相应的表达式。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "z = lambda x: x * x"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "64"
- ]
- },
- "execution_count": 32,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z(8)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(6, 8)"
- ]
- },
- "execution_count": 33,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "zz = lambda x, y: (x*y, x**y)\n",
- "zz(2, 3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "function"
- ]
- },
- "execution_count": 34,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(z)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "function"
- ]
- },
- "execution_count": 35,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "def sqr(x):\n",
- " return x*x\n",
- "\n",
- "type(sqr)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 5.1 map"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**map( )** 函数主要执行分别为列表的每个元素定义的函数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "list1 = [1,2,3,4,5,6,7,8,9]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[3, 4, 5, 6, 7, 8, 9, 10, 11]\n"
- ]
- }
- ],
- "source": [
- "eg = map(lambda x:x+2, list1)\n",
- "print(list(eg))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
- ]
- }
- ],
- "source": [
- "eg = map(sqr, list1)\n",
- "print(list(eg))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "你也可以添加两个列表。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 39,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "list2 = [9,8,7,6,5,4,3,2,1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[10, 10, 10, 10, 10, 10, 10, 10, 10]\n"
- ]
- }
- ],
- "source": [
- "eg2 = map(lambda x,y:x+y, list1,list2)\n",
- "print(list(eg2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "不仅可以使用lambda函数,还可以使用其他内建函数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 41,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<map object at 0x7fba384fd320>\n"
- ]
- }
- ],
- "source": [
- "eg3 = map(str,eg2)\n",
- "print(eg3)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 5.2 filter"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**filter( )** 函数用于过滤列表中的值。 注意 **filter()** 函数返回一个新列表中的结果。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "list1 = [1,2,3,4,5,6,7,8,9]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "为了得到小于5的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4]\n"
- ]
- }
- ],
- "source": [
- "res = filter(lambda x:x<5,list1)\n",
- "print(list(res))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "注意当使用**map()** 时会发生什么"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<map at 0x7fba384fd550>"
- ]
- },
- "execution_count": 44,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "map(lambda x:x<5, list1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "我们可以得出这样的结论,在**map()** 函数中返回的值为真,在使用**filter()** 函数时将返回特定的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 45,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<filter at 0x7fba384fd240>"
- ]
- },
- "execution_count": 45,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "filter(lambda x:x%4==0,list1)"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.5.4"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|