Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Vignesh ran all at 2015-11-30 07:02:18.246032\n"
- ]
- }
- ],
- "source": [
- "student_name = \"Vignesh\" # Chris Boesch\n",
- "\n",
- "\"\"\"\n",
- "Update the student_name variable above with your name. \n",
- "Extend this notebook to solve the problems at the bottom. \n",
- "From the menue Cell -> Run All\n",
- "Save your notebook. \n",
- "Download your notebook. \n",
- "Create a Github Gist of your notebook. \n",
- "Submit a link to your gist in ClassMentors. \n",
- "\n",
- "\"\"\"\n",
- "\n",
- "import datetime\n",
- "now = datetime.datetime.now()\n",
- "\n",
- "message = \"{} ran all at {}\".format(student_name, now)\n",
- "print(message)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4, 5]\n"
- ]
- }
- ],
- "source": [
- "l = [1,2,3,4,5]\n",
- "print(l)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<map object at 0x7f9548e92dd8>\n",
- "[2, 3, 4, 5, 6]\n"
- ]
- }
- ],
- "source": [
- "def add_one(x):\n",
- " return x + 1\n",
- "\n",
- "result = map(add_one,l)\n",
- "\n",
- "print(result)\n",
- "#print(list(result))\n",
- "for item in result: \n",
- " print(item)\n",
- " \n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4, 5]\n"
- ]
- }
- ],
- "source": [
- "print(l)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "8\n",
- "9\n",
- "10\n"
- ]
- }
- ],
- "source": [
- "for item in map(add_one, [7,8,9]):\n",
- " print(item)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<map object at 0x7f9548ec2d68>\n",
- "[19, 14, 9]\n"
- ]
- }
- ],
- "source": [
- "def subtract_one(x): return x - 1\n",
- "\n",
- "result = map(subtract_one, [20, 15, 10])\n",
- "\n",
- "print(result)\n",
- "print( list(result) )"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['0 is even', '1 is odd', '2 is even', '3 is odd', '4 is even']"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "def is_odd(x):\n",
- " if x%2 ==1: \n",
- " return \"{} is odd\".format(x)\n",
- " else: \n",
- " return \"{} is even\".format(x)\n",
- "\n",
- "\n",
- "list( map(is_odd, [0,1,2,3,4]) )"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[False, True, False, True, False]\n"
- ]
- }
- ],
- "source": [
- "def is_odd(x): return x%2==1\n",
- "\n",
- "print(list( map(is_odd, [0,1,2,3,4]) ))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[1, 3, 5]"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# filter\n",
- "# function should return True for False. Only True results are kept. \n",
- "result = filter(is_odd, [0,1,2,3,4,5])\n",
- "list(result)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[7, 11]"
- ]
- },
- "execution_count": 18,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "result = filter(is_odd, map(add_one,[5,6,10,11]))\n",
- "list(result)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[4, 9, 14]"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Lambdas allow us to pass functions around without giving them a name. \n",
- "# Rather than defining subtract_one, I could have just passed the logic in as a lambda. \n",
- "result = map(lambda x: x-1, [5,10,15])\n",
- "list(result)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "4\n",
- "8\n"
- ]
- }
- ],
- "source": [
- "# named function\n",
- "def subtract_one(x): return x - 1\n",
- "\n",
- "# anonymous function assigned to a variable. \n",
- "subtract_two = lambda x: x-2\n",
- "\n",
- "print( subtract_one(5))\n",
- "print( subtract_two(10))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "4\n",
- "8\n",
- "4\n"
- ]
- }
- ],
- "source": [
- "# named function\n",
- "def subtract_one(x): return x - 1\n",
- "f1 = subtract_one\n",
- "\n",
- "# anonymous function assigned to a variable. \n",
- "f2 = lambda x: x-2\n",
- "\n",
- "print( f1(5))\n",
- "print( f2(10))\n",
- "print( subtract_one(5))\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "8\n",
- "9\n"
- ]
- }
- ],
- "source": [
- "r1 = f2(10)\n",
- "r2 = (lambda x: x*x)(3)\n",
- "\n",
- "print(r1)\n",
- "print(r2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[1, 3, 5]"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Get the first item from each list using a lambda. \n",
- "items = [[1,2], [3,4], [5,6]]\n",
- "\n",
- "result = map(lambda x: x[0], items)\n",
- "list(result)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[3, 7, 11]"
- ]
- },
- "execution_count": 28,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Add the 0 and 1 indexed items in each list. \n",
- "items = [[1,2], [3,4], [5,6]]\n",
- "\n",
- "result = map(lambda x: x[0]+x[1], items)\n",
- "list(result)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[2, 4], [4, 16], [6, 36], [8, 64]]\n",
- "[{'square': 4, 'number': 2}, {'square': 16, 'number': 4}, {'square': 36, 'number': 6}, {'square': 64, 'number': 8}]\n"
- ]
- }
- ],
- "source": [
- "# You could also create a list of lists or dictionaries from a list of items. \n",
- "items = [2,4,6,8]\n",
- "\n",
- "result = map(lambda x: [x, x*x], items)\n",
- "print( list( result))\n",
- "\n",
- "result = map(lambda x: {\"number\":x,\"square\":x*x}, items)\n",
- "print( list( result))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[(1, (2, 3)), (4, (5, 6)), (1, (8, 9)), (2, (10, 11))]\n",
- "[(1, 6), (4, 30), (1, 72), (2, 110)]\n"
- ]
- }
- ],
- "source": [
- "# When dealing with CSV data you will often want to have a key to joing by. \n",
- "items = [(1,2,3), (4,5,6), (1,8,9), (2,10,11)]\n",
- "\n",
- "result = map(lambda x: (x[0], (x[1], x[2])), items)\n",
- "new_list = list(result)\n",
- "\n",
- "print( new_list)\n",
- "\n",
- "# Multiply the values together for each key while keeping the key. \n",
- "result = map(lambda x: (x[0], x[1][0]* x[1][1] ), new_list)\n",
- "\n",
- "\n",
- "print( list( result))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[(1, (2, 3)), (4, (5, 6)), (1, (8, 9)), (2, (10, 11))]\n",
- "[(1, 6), (4, 30), (1, 72), (2, 110)]\n",
- "[(1, 72), (2, 110)]\n",
- "------\n",
- "[(1, 72), (2, 110)]\n"
- ]
- }
- ],
- "source": [
- "# When dealing with CSV data you will often want to have a key to joing by. \n",
- "items = [(1,2,3), (4,5,6), (1,8,9), (2,10,11)]\n",
- "\n",
- "result = map(lambda x: (x[0], (x[1], x[2])), items)\n",
- "l1 = list(result)\n",
- "\n",
- "print( l1)\n",
- "\n",
- "# Multiply the values together for each key while keeping the key. \n",
- "result = map(lambda x: (x[0], x[1][0]* x[1][1] ), l1)\n",
- "\n",
- "l2 = list(result)\n",
- "print(l2)\n",
- "\n",
- "result = filter(lambda x: x[1]>50, l2)\n",
- "l3 = list(result)\n",
- "print( l3 ) \n",
- "\n",
- "# Or in one statement you could do all of this at once. \n",
- "print(\"------\")\n",
- "result = filter(lambda x: x[1]>50,\n",
- " map(lambda x: (x[0], x[1][0]* x[1][1] ), \n",
- " map(lambda x: (x[0], (x[1], x[2])), \n",
- " items)))\n",
- "l4 = list(result)\n",
- "print( l4 ) "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 61,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Q1 = 6\n",
- "Q2 = 3\n",
- "Q3 = 2\n",
- "Q4 = [5, 4, 3, 6, 7, 9]\n",
- "Q5 = [6, 5, 8, 9]\n"
- ]
- }
- ],
- "source": [
- "items = [(\"A\",5,6),(\"A\",4,5),(\"A\",3,8),(\"B\",6,9),(\"B\",7,4),(\"C\",9,2)]\n",
- "\"\"\"\n",
- "Write the lambda map and filter functions needed to answer the questions below. \n",
- "\n",
- "Q1 - How many items are there?\n",
- "Q2 - How many A items are there?\n",
- "Q3 - How many B items are there? \n",
- "Q4 - What are 2nd values for all the items? \n",
- "Q5 - What are the 3rd values for items that have a 2nd value greater than 4?\n",
- "\n",
- "\"\"\"\n",
- "\n",
- "\n",
- "q1 = len(items)\n",
- "#print(list(filter(lambda x: x[0]==\"A\", items)))\n",
- "q2 = len(list(filter(lambda x: x[0]==\"A\", items)))\n",
- "q3 = len(list(filter(lambda x: x[0]==\"B\", items)))\n",
- "q4 = list(map(lambda x: x[1] , items))\n",
- "\n",
- "ValidateList = list(filter(lambda x : x[2]>4, items)) \n",
- "q5=list(map(lambda x: x[2], ValidateList))\n",
- "\n",
- "\n",
- "print(\"Q1 = {}\".format(q1))\n",
- "print(\"Q2 = {}\".format(q2))\n",
- "print(\"Q3 = {}\".format(q3))\n",
- "print(\"Q4 = {}\".format(q4))\n",
- "print(\"Q5 = {}\".format(q5))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "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.4.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement