You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

6_Function.ipynb 21 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# 函数"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "在大部分时候,在一个算法中,需要重复执行一组语句,如果每次都重复写出来,不仅乏味而且编程效率比较低,降低程序的可读性。为了将重复的执行抽象出来,可使用函数将一组操作封装成一个整体,给一个名称和参数列表作为可变量的输入。Python中函数的定义为:"
  15. ]
  16. },
  17. {
  18. "cell_type": "markdown",
  19. "metadata": {},
  20. "source": [
  21. "```\n",
  22. "def funcname(arg1, arg2,... argN):\n",
  23. " \n",
  24. " ''' Document String'''\n",
  25. "\n",
  26. " statements\n",
  27. "\n",
  28. "\n",
  29. " return <value>\n",
  30. "```"
  31. ]
  32. },
  33. {
  34. "cell_type": "markdown",
  35. "metadata": {},
  36. "source": [
  37. "将上面的语法理解为,定义了一个名为`funcname`的函数,它接受`arg1,arg2,…argN`的参数。函数在执行语句后返回一个`<value>`。"
  38. ]
  39. },
  40. {
  41. "cell_type": "code",
  42. "execution_count": 1,
  43. "metadata": {},
  44. "outputs": [
  45. {
  46. "name": "stdout",
  47. "output_type": "stream",
  48. "text": [
  49. "Hey Rajath!\n",
  50. "Rajath, How do you do?\n"
  51. ]
  52. }
  53. ],
  54. "source": [
  55. "print(\"Hey Rajath!\")\n",
  56. "print(\"Rajath, How do you do?\")"
  57. ]
  58. },
  59. {
  60. "cell_type": "markdown",
  61. "metadata": {},
  62. "source": [
  63. "不需要每次都写上面的两个语句,可以通过定义一个函数来替换它,这个函数只需一行就能完成任务。\n",
  64. "\n",
  65. "定义一个函数 `first_func()`."
  66. ]
  67. },
  68. {
  69. "cell_type": "code",
  70. "execution_count": 1,
  71. "metadata": {},
  72. "outputs": [],
  73. "source": [
  74. "def first_func():\n",
  75. " print(\"Hey Rajath!\")\n",
  76. " print(\"Rajath, How do you do?\")"
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": 3,
  82. "metadata": {},
  83. "outputs": [
  84. {
  85. "name": "stdout",
  86. "output_type": "stream",
  87. "text": [
  88. "Hey Rajath!\n",
  89. "Rajath, How do you do?\n",
  90. "Hey Rajath!\n",
  91. "Rajath, How do you do?\n"
  92. ]
  93. }
  94. ],
  95. "source": [
  96. "first_func()\n",
  97. "funca=first_func\n",
  98. "funca()"
  99. ]
  100. },
  101. {
  102. "cell_type": "markdown",
  103. "metadata": {},
  104. "source": [
  105. "**first_func()** 每一次只打印一个人的消息。我们可以让我们的函数 **first_func()** 接受参数,该参数将存储名称然后打印相应地接受字符串。为了这样做我们需要像所示的那样在函数内添加一个参数。"
  106. ]
  107. },
  108. {
  109. "cell_type": "code",
  110. "execution_count": 6,
  111. "metadata": {},
  112. "outputs": [],
  113. "source": [
  114. "def first_func(username):\n",
  115. " print(\"Hey\", username + '!')\n",
  116. " print(username + ',' ,\"How do you do?\")"
  117. ]
  118. },
  119. {
  120. "cell_type": "code",
  121. "execution_count": 8,
  122. "metadata": {},
  123. "outputs": [
  124. {
  125. "name": "stdout",
  126. "output_type": "stream",
  127. "text": [
  128. "Please enter your name : hello\n"
  129. ]
  130. }
  131. ],
  132. "source": [
  133. "name1 = input('Please enter your name : ')"
  134. ]
  135. },
  136. {
  137. "cell_type": "markdown",
  138. "metadata": {},
  139. "source": [
  140. "名称“Willam”实际上存储在name1中。我们将这个变量传递给函数**firstfunc()** 作为变量username,因为它是为这个函数定义的变量。即name1作为用户名传递。"
  141. ]
  142. },
  143. {
  144. "cell_type": "code",
  145. "execution_count": 9,
  146. "metadata": {},
  147. "outputs": [
  148. {
  149. "name": "stdout",
  150. "output_type": "stream",
  151. "text": [
  152. "Hey hello!\n",
  153. "hello, How do you do?\n"
  154. ]
  155. }
  156. ],
  157. "source": [
  158. "first_func(name1)"
  159. ]
  160. },
  161. {
  162. "cell_type": "markdown",
  163. "metadata": {},
  164. "source": [
  165. "让我们通过定义另一个函数**second_func()** 来进一步简化它,该函数接受名称并将其存储在一个变量中,然后从函数本身内部调用**first_func()**。"
  166. ]
  167. },
  168. {
  169. "cell_type": "code",
  170. "execution_count": 12,
  171. "metadata": {},
  172. "outputs": [],
  173. "source": [
  174. "def first_func(username):\n",
  175. " print(\"Hey\", username + '!')\n",
  176. " print(username + ',' ,\"How do you do?\")\n",
  177. "def second_func():\n",
  178. " name = input(\"Please enter your name : \")\n",
  179. " first_func(name)"
  180. ]
  181. },
  182. {
  183. "cell_type": "code",
  184. "execution_count": null,
  185. "metadata": {},
  186. "outputs": [],
  187. "source": [
  188. "second_func()"
  189. ]
  190. },
  191. {
  192. "cell_type": "markdown",
  193. "metadata": {},
  194. "source": [
  195. "## 1. 返回语句"
  196. ]
  197. },
  198. {
  199. "cell_type": "markdown",
  200. "metadata": {},
  201. "source": [
  202. "当函数产生某个值,并且该值必须存储在一个变量中,或者需要返回或返回给主算法进行进一步操作时,使用return语句。"
  203. ]
  204. },
  205. {
  206. "cell_type": "code",
  207. "execution_count": 8,
  208. "metadata": {},
  209. "outputs": [],
  210. "source": [
  211. "def times(x,y):\n",
  212. " z = x*y\n",
  213. " return z"
  214. ]
  215. },
  216. {
  217. "cell_type": "markdown",
  218. "metadata": {},
  219. "source": [
  220. "上面定义的**times()** 函数接受两个参数并返回变量z,该变量包含两个参数的乘积结果。"
  221. ]
  222. },
  223. {
  224. "cell_type": "code",
  225. "execution_count": 9,
  226. "metadata": {},
  227. "outputs": [
  228. {
  229. "name": "stdout",
  230. "output_type": "stream",
  231. "text": [
  232. "20\n"
  233. ]
  234. }
  235. ],
  236. "source": [
  237. "c = times(4,5)\n",
  238. "print(c)"
  239. ]
  240. },
  241. {
  242. "cell_type": "markdown",
  243. "metadata": {},
  244. "source": [
  245. "z值存储在变量c中,可以用于进一步的操作。"
  246. ]
  247. },
  248. {
  249. "cell_type": "markdown",
  250. "metadata": {},
  251. "source": [
  252. "可以在return语句中使用整个语句本身,而不是声明另一个变量,如下所示。"
  253. ]
  254. },
  255. {
  256. "cell_type": "code",
  257. "execution_count": 10,
  258. "metadata": {},
  259. "outputs": [],
  260. "source": [
  261. "def times(x,y):\n",
  262. " '''This multiplies the two input arguments'''\n",
  263. " return x*y"
  264. ]
  265. },
  266. {
  267. "cell_type": "code",
  268. "execution_count": 12,
  269. "metadata": {},
  270. "outputs": [
  271. {
  272. "name": "stdout",
  273. "output_type": "stream",
  274. "text": [
  275. "20\n"
  276. ]
  277. }
  278. ],
  279. "source": [
  280. "c = times(4,5)\n",
  281. "print(c)"
  282. ]
  283. },
  284. {
  285. "cell_type": "markdown",
  286. "metadata": {},
  287. "source": [
  288. "由于现在定义了**times()**,我们可以如上所示记录它。在**help()** 函数下调用**times()** 函数时返回该文档。"
  289. ]
  290. },
  291. {
  292. "cell_type": "code",
  293. "execution_count": 13,
  294. "metadata": {},
  295. "outputs": [
  296. {
  297. "name": "stdout",
  298. "output_type": "stream",
  299. "text": [
  300. "Help on function times in module __main__:\n",
  301. "\n",
  302. "times(x, y)\n",
  303. " This multiplies the two input arguments\n",
  304. "\n"
  305. ]
  306. }
  307. ],
  308. "source": [
  309. "help(times)"
  310. ]
  311. },
  312. {
  313. "cell_type": "code",
  314. "execution_count": 14,
  315. "metadata": {},
  316. "outputs": [],
  317. "source": [
  318. "times?"
  319. ]
  320. },
  321. {
  322. "cell_type": "markdown",
  323. "metadata": {},
  324. "source": [
  325. "也可以返回多个变量,但请记住顺序。"
  326. ]
  327. },
  328. {
  329. "cell_type": "code",
  330. "execution_count": 17,
  331. "metadata": {},
  332. "outputs": [],
  333. "source": [
  334. "eglist = [10,50,30,12,6,8,100]"
  335. ]
  336. },
  337. {
  338. "cell_type": "code",
  339. "execution_count": 18,
  340. "metadata": {},
  341. "outputs": [],
  342. "source": [
  343. "def egfunc(eglist):\n",
  344. " highest = max(eglist)\n",
  345. " lowest = min(eglist)\n",
  346. " first = eglist[0]\n",
  347. " last = eglist[-1]\n",
  348. " return highest,lowest,first,last"
  349. ]
  350. },
  351. {
  352. "cell_type": "markdown",
  353. "metadata": {},
  354. "source": [
  355. "如果调用函数时没有为其分配任何变量,则结果将在一个元组中返回。但是如果变量被提到了,那么结果会以特定的顺序分配给变量,这个顺序在return语句中声明。"
  356. ]
  357. },
  358. {
  359. "cell_type": "code",
  360. "execution_count": 22,
  361. "metadata": {},
  362. "outputs": [
  363. {
  364. "name": "stdout",
  365. "output_type": "stream",
  366. "text": [
  367. "(100, 6, 10, 100)\n"
  368. ]
  369. }
  370. ],
  371. "source": [
  372. "a = egfunc(eglist)\n",
  373. "print(a)"
  374. ]
  375. },
  376. {
  377. "cell_type": "code",
  378. "execution_count": 23,
  379. "metadata": {},
  380. "outputs": [
  381. {
  382. "name": "stdout",
  383. "output_type": "stream",
  384. "text": [
  385. " a = 100 \n",
  386. " b = 6 \n",
  387. " c = 10 \n",
  388. " d = 100\n"
  389. ]
  390. }
  391. ],
  392. "source": [
  393. "a,b,c,d = egfunc(eglist)\n",
  394. "print(' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)"
  395. ]
  396. },
  397. {
  398. "cell_type": "markdown",
  399. "metadata": {},
  400. "source": [
  401. "## 2. 隐式参数"
  402. ]
  403. },
  404. {
  405. "cell_type": "markdown",
  406. "metadata": {},
  407. "source": [
  408. "当一个函数的参数在大多数情况下是常见的或者它是`隐式的`时,使用这个概念。"
  409. ]
  410. },
  411. {
  412. "cell_type": "code",
  413. "execution_count": null,
  414. "metadata": {},
  415. "outputs": [],
  416. "source": [
  417. "def implicitadd(x, addnumber=3):\n",
  418. " return x+addnumber"
  419. ]
  420. },
  421. {
  422. "cell_type": "markdown",
  423. "metadata": {},
  424. "source": [
  425. "**implicitadd( )** 是一个函数接受两个参数,但大多数时候第一个参数只需要加3。因此,第二个参数被赋值为3。这里第二个参数是隐式的。"
  426. ]
  427. },
  428. {
  429. "cell_type": "markdown",
  430. "metadata": {},
  431. "source": [
  432. "现在,如果在调用**implicitadd()** 函数时没有定义第二个参数,则将其视为3。"
  433. ]
  434. },
  435. {
  436. "cell_type": "code",
  437. "execution_count": 26,
  438. "metadata": {},
  439. "outputs": [
  440. {
  441. "data": {
  442. "text/plain": [
  443. "7"
  444. ]
  445. },
  446. "execution_count": 26,
  447. "metadata": {},
  448. "output_type": "execute_result"
  449. }
  450. ],
  451. "source": [
  452. "implicitadd(4)"
  453. ]
  454. },
  455. {
  456. "cell_type": "markdown",
  457. "metadata": {},
  458. "source": [
  459. "但如果指定了第二个参数,则此值将覆盖分配给该参数的隐式值"
  460. ]
  461. },
  462. {
  463. "cell_type": "code",
  464. "execution_count": 27,
  465. "metadata": {},
  466. "outputs": [
  467. {
  468. "data": {
  469. "text/plain": [
  470. "8"
  471. ]
  472. },
  473. "execution_count": 27,
  474. "metadata": {},
  475. "output_type": "execute_result"
  476. }
  477. ],
  478. "source": [
  479. "implicitadd(4,4)"
  480. ]
  481. },
  482. {
  483. "cell_type": "code",
  484. "execution_count": 34,
  485. "metadata": {},
  486. "outputs": [
  487. {
  488. "data": {
  489. "text/plain": [
  490. "11"
  491. ]
  492. },
  493. "execution_count": 34,
  494. "metadata": {},
  495. "output_type": "execute_result"
  496. }
  497. ],
  498. "source": [
  499. "implicitadd(5, addnumber=6)"
  500. ]
  501. },
  502. {
  503. "cell_type": "markdown",
  504. "metadata": {},
  505. "source": [
  506. "## 3. 任意数量的参数"
  507. ]
  508. },
  509. {
  510. "cell_type": "markdown",
  511. "metadata": {},
  512. "source": [
  513. "如果函数要接受的参数数量未知,则在参数前使用星号。"
  514. ]
  515. },
  516. {
  517. "cell_type": "code",
  518. "execution_count": 42,
  519. "metadata": {},
  520. "outputs": [],
  521. "source": [
  522. "def add_n(*args):\n",
  523. " res = 0\n",
  524. " reslist = []\n",
  525. " for i in args:\n",
  526. " reslist.append(i)\n",
  527. " print(reslist)\n",
  528. " return sum(reslist)"
  529. ]
  530. },
  531. {
  532. "cell_type": "markdown",
  533. "metadata": {},
  534. "source": [
  535. "上面的函数接受任意数量的参数,定义一个列表,并将所有参数附加到该列表中,并返回所有参数的总和。"
  536. ]
  537. },
  538. {
  539. "cell_type": "code",
  540. "execution_count": 43,
  541. "metadata": {},
  542. "outputs": [
  543. {
  544. "name": "stdout",
  545. "output_type": "stream",
  546. "text": [
  547. "[1, 2, 3, 4, 5]\n"
  548. ]
  549. },
  550. {
  551. "data": {
  552. "text/plain": [
  553. "15"
  554. ]
  555. },
  556. "execution_count": 43,
  557. "metadata": {},
  558. "output_type": "execute_result"
  559. }
  560. ],
  561. "source": [
  562. "add_n(1,2,3,4,5)"
  563. ]
  564. },
  565. {
  566. "cell_type": "code",
  567. "execution_count": 26,
  568. "metadata": {},
  569. "outputs": [
  570. {
  571. "name": "stdout",
  572. "output_type": "stream",
  573. "text": [
  574. "[1, 2, 3]\n"
  575. ]
  576. },
  577. {
  578. "data": {
  579. "text/plain": [
  580. "6"
  581. ]
  582. },
  583. "execution_count": 26,
  584. "metadata": {},
  585. "output_type": "execute_result"
  586. }
  587. ],
  588. "source": [
  589. "add_n(1,2,3)"
  590. ]
  591. },
  592. {
  593. "cell_type": "code",
  594. "execution_count": 46,
  595. "metadata": {},
  596. "outputs": [
  597. {
  598. "name": "stdout",
  599. "output_type": "stream",
  600. "text": [
  601. "[10, 20, 30]\n"
  602. ]
  603. },
  604. {
  605. "data": {
  606. "text/plain": [
  607. "60"
  608. ]
  609. },
  610. "execution_count": 46,
  611. "metadata": {},
  612. "output_type": "execute_result"
  613. }
  614. ],
  615. "source": [
  616. "def add_nd(**kwargs):\n",
  617. " res = 0\n",
  618. " reslist = []\n",
  619. " for (k,v) in kwargs.items():\n",
  620. " reslist.append(v)\n",
  621. " print(reslist)\n",
  622. " return sum(reslist)\n",
  623. "\n",
  624. "add_nd(x=10, y=20, c=30)"
  625. ]
  626. },
  627. {
  628. "cell_type": "markdown",
  629. "metadata": {},
  630. "source": [
  631. "## 4. 全局和局部变量"
  632. ]
  633. },
  634. {
  635. "cell_type": "markdown",
  636. "metadata": {},
  637. "source": [
  638. "在函数内部声明的变量是局部变量,在函数外部声明的是全局变量。"
  639. ]
  640. },
  641. {
  642. "cell_type": "code",
  643. "execution_count": 47,
  644. "metadata": {},
  645. "outputs": [],
  646. "source": [
  647. "eg1 = [1,2,3,4,5]"
  648. ]
  649. },
  650. {
  651. "cell_type": "markdown",
  652. "metadata": {},
  653. "source": [
  654. "在下面的函数中,我们将在函数内部声明的列表中追加一个元素。函数内部声明的eg2变量是一个局部变量。"
  655. ]
  656. },
  657. {
  658. "cell_type": "code",
  659. "execution_count": 48,
  660. "metadata": {},
  661. "outputs": [],
  662. "source": [
  663. "def egfunc1():\n",
  664. " eg1 = [1, 2, 3, 4, 5, 7]\n",
  665. " print(\"egfunc1>> eg1: \", eg1)\n",
  666. "\n",
  667. "def egfunc2():\n",
  668. " eg1.append(8)\n",
  669. " print(\"egfunc2>> eg1: \", eg1)\n",
  670. "\n",
  671. "def egfunc3():\n",
  672. " global eg1\n",
  673. " eg1 = [5, 4, 3, 2, 1]\n",
  674. " print(\"egfunc3>> eg1: \", eg1)"
  675. ]
  676. },
  677. {
  678. "cell_type": "code",
  679. "execution_count": 25,
  680. "metadata": {},
  681. "outputs": [
  682. {
  683. "name": "stdout",
  684. "output_type": "stream",
  685. "text": [
  686. "egfunc1>> eg1: [1, 2, 3, 4, 5, 7]\n",
  687. "eg1: [1, 2, 3, 4, 5] \n",
  688. "\n",
  689. "egfunc2>> eg1: [1, 2, 3, 4, 5, 8]\n",
  690. "eg1: [1, 2, 3, 4, 5, 8] \n",
  691. "\n",
  692. "egfunc3>> eg1: [5, 4, 3, 2, 1]\n",
  693. "eg1: [5, 4, 3, 2, 1] \n",
  694. "\n"
  695. ]
  696. }
  697. ],
  698. "source": [
  699. "egfunc1()\n",
  700. "print(\"eg1: \", eg1, \"\\n\")\n",
  701. "\n",
  702. "egfunc2()\n",
  703. "print(\"eg1: \", eg1, \"\\n\")\n",
  704. "\n",
  705. "egfunc3()\n",
  706. "print(\"eg1: \", eg1, \"\\n\")\n"
  707. ]
  708. },
  709. {
  710. "cell_type": "markdown",
  711. "metadata": {},
  712. "source": [
  713. "## 5. Lambda 函数"
  714. ]
  715. },
  716. {
  717. "cell_type": "markdown",
  718. "metadata": {},
  719. "source": [
  720. "程序中有时需要临时使用一个简单的函数,单独定义出来比较费事,为了提高编程效率,Python等很多语言引入了`Lambda`函数,这些Lambda函数没有使用任何名称定义,只携带一个表达式,返回的是函数本身(类似函数指针或者函数对象)。Lambda函数在操作列表时非常方便。这些函数由关键字**lambda**定义,后面跟着变量、冒号和相应的表达式。"
  721. ]
  722. },
  723. {
  724. "cell_type": "code",
  725. "execution_count": 49,
  726. "metadata": {},
  727. "outputs": [],
  728. "source": [
  729. "z = lambda x: x * x"
  730. ]
  731. },
  732. {
  733. "cell_type": "code",
  734. "execution_count": 50,
  735. "metadata": {},
  736. "outputs": [
  737. {
  738. "data": {
  739. "text/plain": [
  740. "64"
  741. ]
  742. },
  743. "execution_count": 50,
  744. "metadata": {},
  745. "output_type": "execute_result"
  746. }
  747. ],
  748. "source": [
  749. "z(8)"
  750. ]
  751. },
  752. {
  753. "cell_type": "code",
  754. "execution_count": 51,
  755. "metadata": {},
  756. "outputs": [
  757. {
  758. "data": {
  759. "text/plain": [
  760. "(6, 8)"
  761. ]
  762. },
  763. "execution_count": 51,
  764. "metadata": {},
  765. "output_type": "execute_result"
  766. }
  767. ],
  768. "source": [
  769. "zz = lambda x, y: (x*y, x**y)\n",
  770. "zz(2, 3)"
  771. ]
  772. },
  773. {
  774. "cell_type": "code",
  775. "execution_count": 52,
  776. "metadata": {},
  777. "outputs": [
  778. {
  779. "data": {
  780. "text/plain": [
  781. "function"
  782. ]
  783. },
  784. "execution_count": 52,
  785. "metadata": {},
  786. "output_type": "execute_result"
  787. }
  788. ],
  789. "source": [
  790. "type(z)"
  791. ]
  792. },
  793. {
  794. "cell_type": "code",
  795. "execution_count": 53,
  796. "metadata": {},
  797. "outputs": [
  798. {
  799. "data": {
  800. "text/plain": [
  801. "function"
  802. ]
  803. },
  804. "execution_count": 53,
  805. "metadata": {},
  806. "output_type": "execute_result"
  807. }
  808. ],
  809. "source": [
  810. "def sqr(x):\n",
  811. " return x*x\n",
  812. "\n",
  813. "type(sqr)"
  814. ]
  815. },
  816. {
  817. "cell_type": "markdown",
  818. "metadata": {},
  819. "source": [
  820. "### 5.1 map"
  821. ]
  822. },
  823. {
  824. "cell_type": "markdown",
  825. "metadata": {},
  826. "source": [
  827. "**map( )** 函数主要执行分别为列表的每个元素定义的函数。"
  828. ]
  829. },
  830. {
  831. "cell_type": "code",
  832. "execution_count": 54,
  833. "metadata": {},
  834. "outputs": [],
  835. "source": [
  836. "list1 = [1,2,3,4,5,6,7,8,9]"
  837. ]
  838. },
  839. {
  840. "cell_type": "code",
  841. "execution_count": 56,
  842. "metadata": {},
  843. "outputs": [
  844. {
  845. "name": "stdout",
  846. "output_type": "stream",
  847. "text": [
  848. "[3, 4, 5, 6, 7, 8, 9, 10, 11]\n"
  849. ]
  850. }
  851. ],
  852. "source": [
  853. "eg = map(lambda x:x+2, list1)\n",
  854. "print(list(eg))"
  855. ]
  856. },
  857. {
  858. "cell_type": "code",
  859. "execution_count": 59,
  860. "metadata": {},
  861. "outputs": [
  862. {
  863. "name": "stdout",
  864. "output_type": "stream",
  865. "text": [
  866. "[1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
  867. ]
  868. }
  869. ],
  870. "source": [
  871. "eg = map(sqr, list1)\n",
  872. "print(list(eg))"
  873. ]
  874. },
  875. {
  876. "cell_type": "markdown",
  877. "metadata": {},
  878. "source": [
  879. "你也可以添加两个列表。"
  880. ]
  881. },
  882. {
  883. "cell_type": "code",
  884. "execution_count": 60,
  885. "metadata": {},
  886. "outputs": [],
  887. "source": [
  888. "list2 = [9,8,7,6,5,4,3,2,1]"
  889. ]
  890. },
  891. {
  892. "cell_type": "code",
  893. "execution_count": 61,
  894. "metadata": {},
  895. "outputs": [
  896. {
  897. "name": "stdout",
  898. "output_type": "stream",
  899. "text": [
  900. "[10, 10, 10, 10, 10, 10, 10, 10, 10]\n"
  901. ]
  902. }
  903. ],
  904. "source": [
  905. "eg2 = map(lambda x,y:x+y, list1,list2)\n",
  906. "print(list(eg2))"
  907. ]
  908. },
  909. {
  910. "cell_type": "markdown",
  911. "metadata": {},
  912. "source": [
  913. "不仅可以使用lambda函数,还可以使用其他内建函数。"
  914. ]
  915. },
  916. {
  917. "cell_type": "code",
  918. "execution_count": 62,
  919. "metadata": {},
  920. "outputs": [
  921. {
  922. "name": "stdout",
  923. "output_type": "stream",
  924. "text": [
  925. "<map object at 0x7fd754688198>\n"
  926. ]
  927. }
  928. ],
  929. "source": [
  930. "eg3 = map(str,eg2)\n",
  931. "print(eg3)"
  932. ]
  933. },
  934. {
  935. "cell_type": "markdown",
  936. "metadata": {},
  937. "source": [
  938. "### 5.2 filter"
  939. ]
  940. },
  941. {
  942. "cell_type": "markdown",
  943. "metadata": {},
  944. "source": [
  945. "**filter( )** 函数用于过滤列表中的值。 注意 **filter()** 函数返回一个新列表中的结果。"
  946. ]
  947. },
  948. {
  949. "cell_type": "code",
  950. "execution_count": 40,
  951. "metadata": {},
  952. "outputs": [],
  953. "source": [
  954. "list1 = [1,2,3,4,5,6,7,8,9]"
  955. ]
  956. },
  957. {
  958. "cell_type": "markdown",
  959. "metadata": {},
  960. "source": [
  961. "为了得到小于5的元素。"
  962. ]
  963. },
  964. {
  965. "cell_type": "code",
  966. "execution_count": 63,
  967. "metadata": {},
  968. "outputs": [
  969. {
  970. "name": "stdout",
  971. "output_type": "stream",
  972. "text": [
  973. "[1, 2, 3, 4]\n"
  974. ]
  975. }
  976. ],
  977. "source": [
  978. "res = filter(lambda x:x<5,list1)\n",
  979. "print(list(res))"
  980. ]
  981. },
  982. {
  983. "cell_type": "markdown",
  984. "metadata": {},
  985. "source": [
  986. "注意当使用**map()** 时会发生什么"
  987. ]
  988. },
  989. {
  990. "cell_type": "code",
  991. "execution_count": 64,
  992. "metadata": {},
  993. "outputs": [
  994. {
  995. "data": {
  996. "text/plain": [
  997. "<map at 0x7fd7546882b0>"
  998. ]
  999. },
  1000. "execution_count": 64,
  1001. "metadata": {},
  1002. "output_type": "execute_result"
  1003. }
  1004. ],
  1005. "source": [
  1006. "map(lambda x:x<5, list1)"
  1007. ]
  1008. },
  1009. {
  1010. "cell_type": "markdown",
  1011. "metadata": {},
  1012. "source": [
  1013. "我们可以得出这样的结论,在**map()** 函数中返回的值为真,在使用**filter()** 函数时将返回特定的元素。"
  1014. ]
  1015. },
  1016. {
  1017. "cell_type": "code",
  1018. "execution_count": 65,
  1019. "metadata": {},
  1020. "outputs": [
  1021. {
  1022. "data": {
  1023. "text/plain": [
  1024. "<filter at 0x7fd754688320>"
  1025. ]
  1026. },
  1027. "execution_count": 65,
  1028. "metadata": {},
  1029. "output_type": "execute_result"
  1030. }
  1031. ],
  1032. "source": [
  1033. "filter(lambda x:x%4==0,list1)"
  1034. ]
  1035. }
  1036. ],
  1037. "metadata": {
  1038. "kernelspec": {
  1039. "display_name": "Python 3",
  1040. "language": "python",
  1041. "name": "python3"
  1042. },
  1043. "language_info": {
  1044. "codemirror_mode": {
  1045. "name": "ipython",
  1046. "version": 3
  1047. },
  1048. "file_extension": ".py",
  1049. "mimetype": "text/x-python",
  1050. "name": "python",
  1051. "nbconvert_exporter": "python",
  1052. "pygments_lexer": "ipython3",
  1053. "version": "3.7.9"
  1054. }
  1055. },
  1056. "nbformat": 4,
  1057. "nbformat_minor": 1
  1058. }

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。