Advertisement
Guest User

Untitled

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