|
3081 | 3081 | "list(even)" |
3082 | 3082 | ] |
3083 | 3083 | }, |
3084 | | - { |
3085 | | - "cell_type": "markdown", |
3086 | | - "metadata": { |
3087 | | - "slideshow": { |
3088 | | - "slide_type": "slide" |
3089 | | - } |
3090 | | - }, |
3091 | | - "source": [ |
3092 | | - "### Reduce" |
3093 | | - ] |
3094 | | - }, |
3095 | | - { |
3096 | | - "cell_type": "markdown", |
3097 | | - "metadata": { |
3098 | | - "slideshow": { |
3099 | | - "slide_type": "fragment" |
3100 | | - } |
3101 | | - }, |
3102 | | - "source": [ |
3103 | | - "The last functional operator is **`reduce()`**.\n", |
3104 | | - "\n", |
3105 | | - "The job of `reduce(function,list)` is to return a single value that combines all the elements of the list.\n", |
3106 | | - "\n", |
3107 | | - "`reduce(function, list)` sequentially applies `function()` to its previously returned value, and the next element of `list`. \n", |
3108 | | - "\n", |
3109 | | - "For example if `list = [a1,a2,a3,...,a10],` then the first step of \n", |
3110 | | - "`reduce(function, list)` will compute `[function(a1,a2),a3,...,a10]`, and so on.\n", |
3111 | | - "\n", |
3112 | | - "The function supplied to reduce is typically commutative." |
3113 | | - ] |
3114 | | - }, |
3115 | | - { |
3116 | | - "cell_type": "code", |
3117 | | - "execution_count": 129, |
3118 | | - "metadata": { |
3119 | | - "slideshow": { |
3120 | | - "slide_type": "-" |
3121 | | - } |
3122 | | - }, |
3123 | | - "outputs": [ |
3124 | | - { |
3125 | | - "data": { |
3126 | | - "text/plain": [ |
3127 | | - "45" |
3128 | | - ] |
3129 | | - }, |
3130 | | - "execution_count": 129, |
3131 | | - "metadata": {}, |
3132 | | - "output_type": "execute_result" |
3133 | | - } |
3134 | | - ], |
3135 | | - "source": [ |
3136 | | - "from functools import reduce\n", |
3137 | | - "reduce(lambda x,y: x+y, [x for x in range(10)])" |
3138 | | - ] |
3139 | | - }, |
3140 | | - { |
3141 | | - "cell_type": "code", |
3142 | | - "execution_count": 130, |
3143 | | - "metadata": { |
3144 | | - "slideshow": { |
3145 | | - "slide_type": "fragment" |
3146 | | - } |
3147 | | - }, |
3148 | | - "outputs": [ |
3149 | | - { |
3150 | | - "data": { |
3151 | | - "text/plain": [ |
3152 | | - "26" |
3153 | | - ] |
3154 | | - }, |
3155 | | - "execution_count": 130, |
3156 | | - "metadata": {}, |
3157 | | - "output_type": "execute_result" |
3158 | | - } |
3159 | | - ], |
3160 | | - "source": [ |
3161 | | - "reduce (lambda x,y: x if x>y else y, [1, 15, 26, -27])" |
3162 | | - ] |
3163 | | - }, |
3164 | 3084 | { |
3165 | 3085 | "cell_type": "markdown", |
3166 | 3086 | "metadata": { |
|
3325 | 3245 | "anaconda-cloud": {}, |
3326 | 3246 | "celltoolbar": "Slideshow", |
3327 | 3247 | "kernelspec": { |
3328 | | - "display_name": "Python 3 (ipykernel)", |
| 3248 | + "display_name": "Python 3", |
3329 | 3249 | "language": "python", |
3330 | 3250 | "name": "python3" |
3331 | 3251 | }, |
|
3339 | 3259 | "name": "python", |
3340 | 3260 | "nbconvert_exporter": "python", |
3341 | 3261 | "pygments_lexer": "ipython3", |
3342 | | - "version": "3.9.7" |
| 3262 | + "version": "3.7.10" |
3343 | 3263 | }, |
3344 | 3264 | "rise": { |
3345 | 3265 | "scroll": true, |
3346 | | - "theme": "beige" |
| 3266 | + "theme": "beige", |
| 3267 | + "transition": "fade" |
3347 | 3268 | } |
3348 | 3269 | }, |
3349 | 3270 | "nbformat": 4, |
|
0 commit comments