Advertisement
Guest User

Untitled

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