|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# 函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在大部分时候,在一个算法中,语句是不断重复的,一次又一次地执行相同的语句是一项乏味的工作,而且会消耗大量的内存,效率不高。这个时候我们就需要函数。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "这是一个函数的基本语法"
- ]
- },
- {
- "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”的论点。函数被记录为“文档字符串”。函数在执行语句后返回一个“值”。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hey Rajath!\n",
- "Rajath, How do you do?\n"
- ]
- }
- ],
- "source": [
- "print(\"Hey Rajath!\")\n",
- "print(\"Rajath, How do you do?\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "不需要每次都写上面的两个语句,可以通过定义一个函数来替换它,这个函数只需一行就能完成任务。\n",
- "\n",
- "定义一个函数 firstfunc()."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "def firstfunc():\n",
- " print(\"Hey Rajath!\")\n",
- " print(\"Rajath, How do you do?\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hey Rajath!\n",
- "Rajath, How do you do?\n",
- "Hey Rajath!\n",
- "Rajath, How do you do?\n"
- ]
- }
- ],
- "source": [
- "firstfunc()\n",
- "funca=firstfunc\n",
- "funca()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**firstfunc()** 每一次只打印一个人的消息。我们可以让我们的函数 **firstfunc()** 接受参数,该参数将存储名称然后打印相应地接受名词。为了这样做我们需要像所示的那样在函数内添加一个参数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [],
- "source": [
- "def firstfunc(username):\n",
- " print(\"Hey\", username + '!')\n",
- " print(username + ',' ,\"How do you do?\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Please enter your name : Willam\n"
- ]
- }
- ],
- "source": [
- "name1 = input('Please enter your name : ')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "名称“Willam”实际上存储在name1中。我们将这个变量传递给函数**firstfunc()** 作为变量username,因为它是为这个函数定义的变量。即name1作为用户名传递。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hey Willam!\n",
- "Willam, How do you do?\n"
- ]
- }
- ],
- "source": [
- "firstfunc(name1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "让我们通过定义另一个函数**secondfunc()** 来进一步简化它,该函数接受名称并将其存储在一个变量中,然后从函数本身内部调用**firstfunc()**。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "def firstfunc(username):\n",
- " print(\"Hey\", username + '!')\n",
- " print(username + ',' ,\"How do you do?\")\n",
- "def secondfunc():\n",
- " name = input(\"Please enter your name : \")\n",
- " firstfunc(name)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Please enter your name : Tom\n",
- "Hey Tom!\n",
- "Tom, How do you do?\n"
- ]
- }
- ],
- "source": [
- "secondfunc()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 1. 返回语句"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "当函数产生某个值,并且该值必须存储在一个变量中,或者需要返回或返回给主算法进行进一步操作时,使用return语句。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "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": 12,
- "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": 13,
- "metadata": {},
- "outputs": [],
- "source": [
- "def times(x,y):\n",
- " '''This multiplies the two input arguments'''\n",
- " return x*y"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "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": 15,
- "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": 16,
- "metadata": {},
- "outputs": [],
- "source": [
- "times?"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "也可以返回多个变量,但请记住顺序。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [],
- "source": [
- "eglist = [10,50,30,12,6,8,100]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "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": 20,
- "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": 21,
- "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": 25,
- "metadata": {},
- "outputs": [],
- "source": [
- "def implicitadd(x,y=3):\n",
- " return x+y"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**implicitadd( )** 是一个函数接受两个参数,但大多数时候第一个参数只需要加3。因此,第二个参数被赋值为3。这里第二个参数是隐式的。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在,如果在调用**implicitadd()** 函数时没有定义第二个参数,则将其视为3。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "7"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicitadd(4)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "但如果指定了第二个参数,则此值将覆盖分配给该参数的隐式值"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "8"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicitadd(4,4)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "11"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicitadd(5, y=6)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 3. 任意数量的参数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果函数要接受的参数数量未知,则在参数前使用星号。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {},
- "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": 30,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4, 5]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "15"
- ]
- },
- "execution_count": 30,
- "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": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[30, 10, 20]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "60"
- ]
- },
- "execution_count": 31,
- "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": 10,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1 = [1,2,3,4,5]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在下面的函数中,我们将在函数内部声明的列表中追加一个元素。函数内部声明的eg2变量是一个局部变量。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "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": 12,
- "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": [
- "这些小函数没有使用任何名称定义,只携带一个表达式,返回其结果。Lambda函数在操作列表时非常方便。这些函数由关键字**lambda**定义,后面跟着变量、冒号和相应的表达式。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {},
- "outputs": [],
- "source": [
- "z = lambda x: x * x"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "64"
- ]
- },
- "execution_count": 49,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z(8)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(6, 8)"
- ]
- },
- "execution_count": 50,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "zz = lambda x, y: (x*y, x**y)\n",
- "zz(2, 3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "function"
- ]
- },
- "execution_count": 51,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(z)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "function"
- ]
- },
- "execution_count": 52,
- "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": 53,
- "metadata": {},
- "outputs": [],
- "source": [
- "list1 = [1,2,3,4,5,6,7,8,9]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "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": 55,
- "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": 34,
- "metadata": {},
- "outputs": [],
- "source": [
- "list2 = [9,8,7,6,5,4,3,2,1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "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(eg2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "不仅可以使用lambda函数,还可以使用其他内建函数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['10', '10', '10', '10', '10', '10', '10', '10', '10']\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": 56,
- "metadata": {},
- "outputs": [],
- "source": [
- "list1 = [1,2,3,4,5,6,7,8,9]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "为了得到小于5的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 57,
- "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": 58,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<map at 0x7f72a418c780>"
- ]
- },
- "execution_count": 58,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "map(lambda x:x<5, list1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "我们可以得出这样的结论,在**map()** 函数中返回的值为真,在使用**filter()** 函数时将返回特定的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 59,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<filter at 0x7f72a4195240>"
- ]
- },
- "execution_count": 59,
- "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.6.8"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|