{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "
\n",
" Lambda functions are often used in map
, filter
, and sort
. They are analogous to Mathematica pure functions. A pure function such as {#,#^2}&
in Mathematica would be the lambda function lambda k:[k,k^2]
in Python/SageMath.\n",
"
\n",
" The list [a,b]
and the tuple (a,b)
are different. However, either can be used in plotting.\n",
"
\n", " The following expression doesn't produce an output but does act on the list.\n", "
" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "data.sort()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 522, 527, 555, 623, 641, 717, 741, 791, 899]" ] }, "execution_count": 12, "metadata": { }, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "data.sort(key=lambda k:-(k%10))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[899, 527, 717, 555, 623, 522, 1, 641, 741, 791]" ] }, "execution_count": 17, "metadata": { }, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ ] } ], "metadata": { "kernelspec": { "display_name": "SageMath (stable)", "language": "sagemath", "metadata": { "cocalc": { "description": "Open-source mathematical software system", "priority": 10, "url": "https://www.sagemath.org/" } }, "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 0 }