|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058 |
- {
- "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 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",
- "定义一个函数 `first_func()`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "def first_func():\n",
- " print(\"Hey Rajath!\")\n",
- " print(\"Rajath, How do you do?\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "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": [
- "first_func()\n",
- "funca=first_func\n",
- "funca()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**first_func()** 每一次只打印一个人的消息。我们可以让我们的函数 **first_func()** 接受参数,该参数将存储名称然后打印相应地接受字符串。为了这样做我们需要像所示的那样在函数内添加一个参数。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [],
- "source": [
- "def first_func(username):\n",
- " print(\"Hey\", username + '!')\n",
- " print(username + ',' ,\"How do you do?\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Please enter your name : hello\n"
- ]
- }
- ],
- "source": [
- "name1 = input('Please enter your name : ')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "名称“Willam”实际上存储在name1中。我们将这个变量传递给函数**firstfunc()** 作为变量username,因为它是为这个函数定义的变量。即name1作为用户名传递。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Hey hello!\n",
- "hello, How do you do?\n"
- ]
- }
- ],
- "source": [
- "first_func(name1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "让我们通过定义另一个函数**second_func()** 来进一步简化它,该函数接受名称并将其存储在一个变量中,然后从函数本身内部调用**first_func()**。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "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": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "second_func()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 1. 返回语句"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "当函数产生某个值,并且该值必须存储在一个变量中,或者需要返回或返回给主算法进行进一步操作时,使用return语句。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "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": 9,
- "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": 10,
- "metadata": {},
- "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": {},
- "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": 18,
- "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": 22,
- "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": 23,
- "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": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "def implicitadd(x, addnumber=3):\n",
- " return x+addnumber"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**implicitadd( )** 是一个函数接受两个参数,但大多数时候第一个参数只需要加3。因此,第二个参数被赋值为3。这里第二个参数是隐式的。"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "现在,如果在调用**implicitadd()** 函数时没有定义第二个参数,则将其视为3。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "7"
- ]
- },
- "execution_count": 26,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicitadd(4)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "但如果指定了第二个参数,则此值将覆盖分配给该参数的隐式值"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "8"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicitadd(4,4)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "11"
- ]
- },
- "execution_count": 34,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "implicitadd(5, addnumber=6)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 3. 任意数量的参数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "如果函数要接受的参数数量未知,则在参数前使用星号。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "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": 43,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4, 5]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "15"
- ]
- },
- "execution_count": 43,
- "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": 46,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[10, 20, 30]\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "60"
- ]
- },
- "execution_count": 46,
- "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": 47,
- "metadata": {},
- "outputs": [],
- "source": [
- "eg1 = [1,2,3,4,5]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "在下面的函数中,我们将在函数内部声明的列表中追加一个元素。函数内部声明的eg2变量是一个局部变量。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "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": 25,
- "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": 49,
- "metadata": {},
- "outputs": [],
- "source": [
- "z = lambda x: x * x"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "64"
- ]
- },
- "execution_count": 50,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z(8)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(6, 8)"
- ]
- },
- "execution_count": 51,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "zz = lambda x, y: (x*y, x**y)\n",
- "zz(2, 3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "function"
- ]
- },
- "execution_count": 52,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(z)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 53,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "function"
- ]
- },
- "execution_count": 53,
- "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": 54,
- "metadata": {},
- "outputs": [],
- "source": [
- "list1 = [1,2,3,4,5,6,7,8,9]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 56,
- "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": 59,
- "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": 60,
- "metadata": {},
- "outputs": [],
- "source": [
- "list2 = [9,8,7,6,5,4,3,2,1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 61,
- "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": 62,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<map object at 0x7fd754688198>\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": 40,
- "metadata": {},
- "outputs": [],
- "source": [
- "list1 = [1,2,3,4,5,6,7,8,9]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "为了得到小于5的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 63,
- "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": 64,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<map at 0x7fd7546882b0>"
- ]
- },
- "execution_count": 64,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "map(lambda x:x<5, list1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "我们可以得出这样的结论,在**map()** 函数中返回的值为真,在使用**filter()** 函数时将返回特定的元素。"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 65,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<filter at 0x7fd754688320>"
- ]
- },
- "execution_count": 65,
- "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.7.9"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|