Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 30,
  6. "metadata": {
  7. "collapsed": false
  8. },
  9. "outputs": [
  10. {
  11. "name": "stdout",
  12. "output_type": "stream",
  13. "text": [
  14. "Wan Xulang ran all at 2015-11-30 11:26:13.161571\n"
  15. ]
  16. }
  17. ],
  18. "source": [
  19. "student_name = \"Wan Xulang\" # Chris Boesch\n",
  20. "\n",
  21. "\"\"\"\n",
  22. "Update the student_name variable above with your name. \n",
  23. "Extend this notebook to solve the problems at the bottom. \n",
  24. "From the menue Cell -> Run All\n",
  25. "Save your notebook. \n",
  26. "Download your notebook. \n",
  27. "Create a Github Gist of your notebook. \n",
  28. "Submit a link to your gist in ClassMentors. \n",
  29. "\n",
  30. "\"\"\"\n",
  31. "\n",
  32. "import datetime\n",
  33. "now = datetime.datetime.now()\n",
  34. "\n",
  35. "message = \"{} ran all at {}\".format(student_name, now)\n",
  36. "print(message)"
  37. ]
  38. },
  39. {
  40. "cell_type": "code",
  41. "execution_count": 31,
  42. "metadata": {
  43. "collapsed": false
  44. },
  45. "outputs": [
  46. {
  47. "name": "stdout",
  48. "output_type": "stream",
  49. "text": [
  50. "[1, 2, 3, 4, 5]\n"
  51. ]
  52. }
  53. ],
  54. "source": [
  55. "l = [1,2,3,4,5]\n",
  56. "print(l)"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": 32,
  62. "metadata": {
  63. "collapsed": false
  64. },
  65. "outputs": [
  66. {
  67. "name": "stdout",
  68. "output_type": "stream",
  69. "text": [
  70. "<map object at 0x7fbcfc07d6a0>\n",
  71. "2\n",
  72. "3\n",
  73. "4\n",
  74. "5\n",
  75. "6\n"
  76. ]
  77. }
  78. ],
  79. "source": [
  80. "def add_one(x):\n",
  81. " return x + 1\n",
  82. "\n",
  83. "result = map(add_one,l)\n",
  84. "\n",
  85. "print(result)\n",
  86. "for item in result: \n",
  87. " print(item)"
  88. ]
  89. },
  90. {
  91. "cell_type": "code",
  92. "execution_count": 33,
  93. "metadata": {
  94. "collapsed": false
  95. },
  96. "outputs": [
  97. {
  98. "name": "stdout",
  99. "output_type": "stream",
  100. "text": [
  101. "[1, 2, 3, 4, 5]\n"
  102. ]
  103. }
  104. ],
  105. "source": [
  106. "# The original list has not been changed\n",
  107. "print(l)"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": 34,
  113. "metadata": {
  114. "collapsed": false
  115. },
  116. "outputs": [
  117. {
  118. "name": "stdout",
  119. "output_type": "stream",
  120. "text": [
  121. "8\n",
  122. "9\n",
  123. "10\n"
  124. ]
  125. }
  126. ],
  127. "source": [
  128. "for item in map(add_one, [7,8,9]):\n",
  129. " print(item)"
  130. ]
  131. },
  132. {
  133. "cell_type": "code",
  134. "execution_count": 35,
  135. "metadata": {
  136. "collapsed": false
  137. },
  138. "outputs": [
  139. {
  140. "name": "stdout",
  141. "output_type": "stream",
  142. "text": [
  143. "<map object at 0x7fbcfc082320>\n",
  144. "[19, 14, 9]\n"
  145. ]
  146. }
  147. ],
  148. "source": [
  149. "def subtract_one(x): return x - 1\n",
  150. "\n",
  151. "result = map(subtract_one, [20, 15, 10])\n",
  152. "\n",
  153. "print(result)\n",
  154. "print( list(result) )"
  155. ]
  156. },
  157. {
  158. "cell_type": "code",
  159. "execution_count": 36,
  160. "metadata": {
  161. "collapsed": false
  162. },
  163. "outputs": [
  164. {
  165. "data": {
  166. "text/plain": [
  167. "['0 is even', '1 is odd', '2 is even', '3 is odd', '4 is even']"
  168. ]
  169. },
  170. "execution_count": 36,
  171. "metadata": {},
  172. "output_type": "execute_result"
  173. }
  174. ],
  175. "source": [
  176. "def is_odd(x):\n",
  177. " if x%2 ==1: \n",
  178. " return \"{} is odd\".format(x)\n",
  179. " else: \n",
  180. " return \"{} is even\".format(x)\n",
  181. "\n",
  182. "\n",
  183. "list( map(is_odd, [0,1,2,3,4]) )"
  184. ]
  185. },
  186. {
  187. "cell_type": "code",
  188. "execution_count": 37,
  189. "metadata": {
  190. "collapsed": false
  191. },
  192. "outputs": [
  193. {
  194. "data": {
  195. "text/plain": [
  196. "[False, True, False, True, False]"
  197. ]
  198. },
  199. "execution_count": 37,
  200. "metadata": {},
  201. "output_type": "execute_result"
  202. }
  203. ],
  204. "source": [
  205. "def is_odd(x): return x%2==1\n",
  206. "\n",
  207. "list( map(is_odd, [0,1,2,3,4]) )"
  208. ]
  209. },
  210. {
  211. "cell_type": "code",
  212. "execution_count": 38,
  213. "metadata": {
  214. "collapsed": false
  215. },
  216. "outputs": [
  217. {
  218. "data": {
  219. "text/plain": [
  220. "[1, 3, 5]"
  221. ]
  222. },
  223. "execution_count": 38,
  224. "metadata": {},
  225. "output_type": "execute_result"
  226. }
  227. ],
  228. "source": [
  229. "# filter\n",
  230. "# function should return True for False. Only True results are kept. \n",
  231. "result = filter(is_odd, [0,1,2,3,4,5])\n",
  232. "list(result)\n"
  233. ]
  234. },
  235. {
  236. "cell_type": "code",
  237. "execution_count": 39,
  238. "metadata": {
  239. "collapsed": false
  240. },
  241. "outputs": [
  242. {
  243. "data": {
  244. "text/plain": [
  245. "[7, 11]"
  246. ]
  247. },
  248. "execution_count": 39,
  249. "metadata": {},
  250. "output_type": "execute_result"
  251. }
  252. ],
  253. "source": [
  254. "# Filter and map can be combined. \n",
  255. "\n",
  256. "result = filter(is_odd, map(add_one,[5,6,10,11]))\n",
  257. "list(result)\n"
  258. ]
  259. },
  260. {
  261. "cell_type": "code",
  262. "execution_count": 40,
  263. "metadata": {
  264. "collapsed": false
  265. },
  266. "outputs": [
  267. {
  268. "data": {
  269. "text/plain": [
  270. "[4, 9, 14]"
  271. ]
  272. },
  273. "execution_count": 40,
  274. "metadata": {},
  275. "output_type": "execute_result"
  276. }
  277. ],
  278. "source": [
  279. "# Lambdas allow us to pass functions around without giving them a name. \n",
  280. "# Rather than defining subtract_one, I could have just passed the logic in as a lambda. \n",
  281. "result = map(lambda x: x-1, [5,10,15])\n",
  282. "list(result)"
  283. ]
  284. },
  285. {
  286. "cell_type": "code",
  287. "execution_count": 41,
  288. "metadata": {
  289. "collapsed": false
  290. },
  291. "outputs": [
  292. {
  293. "name": "stdout",
  294. "output_type": "stream",
  295. "text": [
  296. "4\n",
  297. "8\n"
  298. ]
  299. }
  300. ],
  301. "source": [
  302. "# named function\n",
  303. "def subtract_one(x): return x - 1\n",
  304. "\n",
  305. "# anonymous function assigned to a variable. \n",
  306. "subtract_two = lambda x: x-2\n",
  307. "\n",
  308. "print( subtract_one(5))\n",
  309. "print( subtract_two(10))\n"
  310. ]
  311. },
  312. {
  313. "cell_type": "code",
  314. "execution_count": 42,
  315. "metadata": {
  316. "collapsed": false
  317. },
  318. "outputs": [
  319. {
  320. "name": "stdout",
  321. "output_type": "stream",
  322. "text": [
  323. "4\n",
  324. "8\n"
  325. ]
  326. }
  327. ],
  328. "source": [
  329. "# named function\n",
  330. "def subtract_one(x): return x - 1\n",
  331. "f1 = subtract_one\n",
  332. "\n",
  333. "# anonymous function assigned to a variable. \n",
  334. "f2 = lambda x: x-2\n",
  335. "\n",
  336. "print( f1(5))\n",
  337. "print( f2(10))\n"
  338. ]
  339. },
  340. {
  341. "cell_type": "code",
  342. "execution_count": 43,
  343. "metadata": {
  344. "collapsed": true
  345. },
  346. "outputs": [],
  347. "source": [
  348. "#def add(x,y): return x+y"
  349. ]
  350. },
  351. {
  352. "cell_type": "code",
  353. "execution_count": 44,
  354. "metadata": {
  355. "collapsed": false
  356. },
  357. "outputs": [
  358. {
  359. "name": "stdout",
  360. "output_type": "stream",
  361. "text": [
  362. "8\n",
  363. "9\n"
  364. ]
  365. }
  366. ],
  367. "source": [
  368. "r1 = f2(10)\n",
  369. "r2 = (lambda x: x*x)(3)\n",
  370. "\n",
  371. "print(r1)\n",
  372. "print(r2)"
  373. ]
  374. },
  375. {
  376. "cell_type": "code",
  377. "execution_count": 45,
  378. "metadata": {
  379. "collapsed": false
  380. },
  381. "outputs": [
  382. {
  383. "data": {
  384. "text/plain": [
  385. "[1, 3, 5]"
  386. ]
  387. },
  388. "execution_count": 45,
  389. "metadata": {},
  390. "output_type": "execute_result"
  391. }
  392. ],
  393. "source": [
  394. "# Get the first item from each list using a lambda. \n",
  395. "items = [[1,2], [3,4], [5,6]]\n",
  396. "\n",
  397. "result = map(lambda x: x[0], items)\n",
  398. "list(result)"
  399. ]
  400. },
  401. {
  402. "cell_type": "code",
  403. "execution_count": 46,
  404. "metadata": {
  405. "collapsed": false
  406. },
  407. "outputs": [
  408. {
  409. "data": {
  410. "text/plain": [
  411. "[3, 7, 11]"
  412. ]
  413. },
  414. "execution_count": 46,
  415. "metadata": {},
  416. "output_type": "execute_result"
  417. }
  418. ],
  419. "source": [
  420. "# Add the 0 and 1 indexed items in each list. \n",
  421. "items = [[1,2], [3,4], [5,6]]\n",
  422. "\n",
  423. "result = map(lambda x: x[0]+x[1], items)\n",
  424. "list(result)"
  425. ]
  426. },
  427. {
  428. "cell_type": "code",
  429. "execution_count": 47,
  430. "metadata": {
  431. "collapsed": false
  432. },
  433. "outputs": [
  434. {
  435. "name": "stdout",
  436. "output_type": "stream",
  437. "text": [
  438. "[[2, 4], [4, 16], [6, 36], [8, 64]]\n",
  439. "[{'number': 2, 'square': 4}, {'number': 4, 'square': 16}, {'number': 6, 'square': 36}, {'number': 8, 'square': 64}]\n"
  440. ]
  441. }
  442. ],
  443. "source": [
  444. "# You could also create a list of lists or dictionaries from a list of items. \n",
  445. "items = [2,4,6,8]\n",
  446. "\n",
  447. "result = map(lambda x: [x, x*x], items)\n",
  448. "print( list( result))\n",
  449. "\n",
  450. "result = map(lambda x: {\"number\":x,\"square\":x*x}, items)\n",
  451. "print( list( result))"
  452. ]
  453. },
  454. {
  455. "cell_type": "code",
  456. "execution_count": 48,
  457. "metadata": {
  458. "collapsed": false
  459. },
  460. "outputs": [
  461. {
  462. "name": "stdout",
  463. "output_type": "stream",
  464. "text": [
  465. "[(1, (2, 3)), (4, (5, 6)), (1, (8, 9)), (2, (10, 11))]\n",
  466. "[(1, 6), (4, 30), (1, 72), (2, 110)]\n"
  467. ]
  468. }
  469. ],
  470. "source": [
  471. "# When dealing with CSV data you will often want to have a key to joing by. \n",
  472. "items = [(1,2,3), (4,5,6), (1,8,9), (2,10,11)]\n",
  473. "\n",
  474. "result = map(lambda x: (x[0], (x[1], x[2])), items)\n",
  475. "new_list = list(result)\n",
  476. "\n",
  477. "print( new_list)\n",
  478. "\n",
  479. "# Multiply the values together for each key while keeping the key. \n",
  480. "result = map(lambda x: (x[0], x[1][0]* x[1][1] ), new_list)\n",
  481. "\n",
  482. "\n",
  483. "print( list( result))\n",
  484. "\n"
  485. ]
  486. },
  487. {
  488. "cell_type": "code",
  489. "execution_count": 49,
  490. "metadata": {
  491. "collapsed": false
  492. },
  493. "outputs": [
  494. {
  495. "name": "stdout",
  496. "output_type": "stream",
  497. "text": [
  498. "[(1, (2, 3)), (4, (5, 6)), (1, (8, 9)), (2, (10, 11))]\n",
  499. "[(1, 6), (4, 30), (1, 72), (2, 110)]\n",
  500. "[(1, 72), (2, 110)]\n",
  501. "------\n",
  502. "[(1, 72), (2, 110)]\n"
  503. ]
  504. }
  505. ],
  506. "source": [
  507. "# When dealing with CSV data you will often want to have a key to joing by. \n",
  508. "items = [(1,2,3), (4,5,6), (1,8,9), (2,10,11)]\n",
  509. "\n",
  510. "result = map(lambda x: (x[0], (x[1], x[2])), items)\n",
  511. "l1 = list(result)\n",
  512. "\n",
  513. "print( l1)\n",
  514. "\n",
  515. "# Multiply the values together for each key while keeping the key. \n",
  516. "result = map(lambda x: (x[0], x[1][0]* x[1][1] ), l1)\n",
  517. "\n",
  518. "l2 = list(result)\n",
  519. "print(l2)\n",
  520. "\n",
  521. "result = filter(lambda x: x[1]>50, l2)\n",
  522. "l3 = list(result)\n",
  523. "print( l3 ) \n",
  524. "\n",
  525. "# Or in one statement you could do all of this at once. \n",
  526. "print(\"------\")\n",
  527. "result = filter(lambda x: x[1]>50,\n",
  528. " map(lambda x: (x[0], x[1][0]* x[1][1] ), \n",
  529. " map(lambda x: (x[0], (x[1], x[2])), \n",
  530. " items)))\n",
  531. "l4 = list(result)\n",
  532. "print( l4 ) \n"
  533. ]
  534. },
  535. {
  536. "cell_type": "code",
  537. "execution_count": 50,
  538. "metadata": {
  539. "collapsed": false,
  540. "scrolled": true
  541. },
  542. "outputs": [
  543. {
  544. "name": "stdout",
  545. "output_type": "stream",
  546. "text": [
  547. "Q1 = 6\n",
  548. "Q2 = 3\n",
  549. "Q3 = 2\n",
  550. "Q4 = [5, 4, 3, 6, 7, 9]\n",
  551. "Q5 = [6, 9, 4, 2]\n"
  552. ]
  553. }
  554. ],
  555. "source": [
  556. "items = [(\"A\",5,6),(\"A\",4,5),(\"A\",3,8),(\"B\",6,9),(\"B\",7,4),(\"C\",9,2)]\n",
  557. "\"\"\"\n",
  558. "Write the lambda map and filter functions needed to answer the questions below. \n",
  559. "\n",
  560. "Q1 - How many items are there?\n",
  561. "Q2 - How many A items are there?\n",
  562. "Q3 - How many B items are there? \n",
  563. "Q4 - What are 2nd values for all the items? \n",
  564. "Q5 - What are the 3rd values for items that have a 2nd value greater than 4?\n",
  565. "\n",
  566. "\"\"\"\n",
  567. "\n",
  568. "\n",
  569. "q1 = len(items)\n",
  570. "q2 = len(list(filter(lambda x: x[0]==\"A\",items)))\n",
  571. "q3 = len(list(filter(lambda x: x[0]==\"B\",items)))\n",
  572. "q4 = list(map(lambda x:x[1],items))\n",
  573. "q5 = list(map(lambda x:x[2],filter(lambda x:x[1]>4,items)))\n",
  574. "\n",
  575. "print(\"Q1 = {}\".format(q1))\n",
  576. "print(\"Q2 = {}\".format(q2))\n",
  577. "print(\"Q3 = {}\".format(q3))\n",
  578. "print(\"Q4 = {}\".format(q4))\n",
  579. "print(\"Q5 = {}\".format(q5))"
  580. ]
  581. },
  582. {
  583. "cell_type": "code",
  584. "execution_count": null,
  585. "metadata": {
  586. "collapsed": true
  587. },
  588. "outputs": [],
  589. "source": []
  590. },
  591. {
  592. "cell_type": "code",
  593. "execution_count": null,
  594. "metadata": {
  595. "collapsed": true
  596. },
  597. "outputs": [],
  598. "source": []
  599. },
  600. {
  601. "cell_type": "code",
  602. "execution_count": null,
  603. "metadata": {
  604. "collapsed": true
  605. },
  606. "outputs": [],
  607. "source": []
  608. }
  609. ],
  610. "metadata": {
  611. "kernelspec": {
  612. "display_name": "Python 3",
  613. "language": "python",
  614. "name": "python3"
  615. },
  616. "language_info": {
  617. "codemirror_mode": {
  618. "name": "ipython",
  619. "version": 3
  620. },
  621. "file_extension": ".py",
  622. "mimetype": "text/x-python",
  623. "name": "python",
  624. "nbconvert_exporter": "python",
  625. "pygments_lexer": "ipython3",
  626. "version": "3.4.3"
  627. }
  628. },
  629. "nbformat": 4,
  630. "nbformat_minor": 0
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement