diff --git a/01. python tools/01.02 ipython interpreter.ipynb b/01. python tools/01.02 ipython interpreter.ipynb index 61fe3e65..1cf4f514 100644 --- a/01. python tools/01.02 ipython interpreter.ipynb +++ b/01. python tools/01.02 ipython interpreter.ipynb @@ -1,867 +1,867 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Ipython 解释器" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 进入ipython" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "通常我们并不使用**Python**自带的解释器,而是使用另一个比较方便的解释器——**ipython**解释器,命令行下输入:\n", - "\n", - " ipython\n", - "\n", - "即可进入**ipython**解释器。\n", - "\n", - "所有在**python**解释器下可以运行的代码都可以在**ipython**解释器下运行:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello, world\n" - ] - } - ], - "source": [ - "print \"hello, world\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以进行简单赋值操作:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "a = 1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "直接在解释器中输入变量名,会显示变量的值(不需要加`print`):" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "b = [1, 2, 3]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##ipython magic命令" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**ipython**解释器提供了很多以百分号`%`开头的`magic`命令,这些命令很像linux系统下的命令行命令(事实上有些是一样的)。\n", - "\n", - "查看所有的`magic`命令:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "application/json": { - "cell": { - "!": "OSMagics", - "HTML": "Other", - "SVG": "Other", - "bash": "Other", - "capture": "ExecutionMagics", - "cmd": "Other", - "debug": "ExecutionMagics", - "file": "Other", - "html": "DisplayMagics", - "javascript": "DisplayMagics", - "latex": "DisplayMagics", - "perl": "Other", - "prun": "ExecutionMagics", - "pypy": "Other", - "python": "Other", - "python2": "Other", - "python3": "Other", - "ruby": "Other", - "script": "ScriptMagics", - "sh": "Other", - "svg": "DisplayMagics", - "sx": "OSMagics", - "system": "OSMagics", - "time": "ExecutionMagics", - "timeit": "ExecutionMagics", - "writefile": "OSMagics" - }, - "line": { - "alias": "OSMagics", - "alias_magic": "BasicMagics", - "autocall": "AutoMagics", - "automagic": "AutoMagics", - "autosave": "KernelMagics", - "bookmark": "OSMagics", - "cd": "OSMagics", - "clear": "KernelMagics", - "cls": "KernelMagics", - "colors": "BasicMagics", - "config": "ConfigMagics", - "connect_info": "KernelMagics", - "copy": "Other", - "ddir": "Other", - "debug": "ExecutionMagics", - "dhist": "OSMagics", - "dirs": "OSMagics", - "doctest_mode": "BasicMagics", - "echo": "Other", - "ed": "Other", - "edit": "KernelMagics", - "env": "OSMagics", - "gui": "BasicMagics", - "hist": "Other", - "history": "HistoryMagics", - "install_default_config": "DeprecatedMagics", - "install_ext": "ExtensionMagics", - "install_profiles": "DeprecatedMagics", - "killbgscripts": "ScriptMagics", - "ldir": "Other", - "less": "KernelMagics", - "load": "CodeMagics", - "load_ext": "ExtensionMagics", - "loadpy": "CodeMagics", - "logoff": "LoggingMagics", - "logon": "LoggingMagics", - "logstart": "LoggingMagics", - "logstate": "LoggingMagics", - "logstop": "LoggingMagics", - "ls": "Other", - "lsmagic": "BasicMagics", - "macro": "ExecutionMagics", - "magic": "BasicMagics", - "matplotlib": "PylabMagics", - "mkdir": "Other", - "more": "KernelMagics", - "notebook": "BasicMagics", - "page": "BasicMagics", - "pastebin": "CodeMagics", - "pdb": "ExecutionMagics", - "pdef": "NamespaceMagics", - "pdoc": "NamespaceMagics", - "pfile": "NamespaceMagics", - "pinfo": "NamespaceMagics", - "pinfo2": "NamespaceMagics", - "popd": "OSMagics", - "pprint": "BasicMagics", - "precision": "BasicMagics", - "profile": "BasicMagics", - "prun": "ExecutionMagics", - "psearch": "NamespaceMagics", - "psource": "NamespaceMagics", - "pushd": "OSMagics", - "pwd": "OSMagics", - "pycat": "OSMagics", - "pylab": "PylabMagics", - "qtconsole": "KernelMagics", - "quickref": "BasicMagics", - "recall": "HistoryMagics", - "rehashx": "OSMagics", - "reload_ext": "ExtensionMagics", - "ren": "Other", - "rep": "Other", - "rerun": "HistoryMagics", - "reset": "NamespaceMagics", - "reset_selective": "NamespaceMagics", - "rmdir": "Other", - "run": "ExecutionMagics", - "save": "CodeMagics", - "sc": "OSMagics", - "set_env": "OSMagics", - "store": "StoreMagics", - "sx": "OSMagics", - "system": "OSMagics", - "tb": "ExecutionMagics", - "time": "ExecutionMagics", - "timeit": "ExecutionMagics", - "unalias": "OSMagics", - "unload_ext": "ExtensionMagics", - "who": "NamespaceMagics", - "who_ls": "NamespaceMagics", - "whos": "NamespaceMagics", - "xdel": "NamespaceMagics", - "xmode": "BasicMagics" - } - }, - "text/plain": [ - "Available line magics:\n", - "%alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n", - "\n", - "Available cell magics:\n", - "%%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n", - "\n", - "Automagic is ON, % prefix IS NOT needed for line magics." - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "%lsmagic" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`line magic` 以一个百分号开头,作用与一行;\n", - "\n", - "`cell magic` 以两个百分号开头,作用于整个cell。\n", - "\n", - "最后一行`Automagic is ON, % prefix IS NOT needed for line magics.`说明在此时即使不加上`%`也可以使用这些命令。\n", - "\n", - "使用 `whos` 查看当前的变量空间:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Variable Type Data/Info\n", - "----------------------------\n", - "a int 1\n", - "b list n=3\n" - ] - } - ], - "source": [ - "%whos" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `reset` 重置当前变量空间:" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "%reset -f" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "再查看当前变量空间:" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Interactive namespace is empty.\n" - ] - } - ], - "source": [ - "%whos" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `pwd` 查看当前工作文件夹:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "u'C:\\\\Users\\\\lijin\\\\Documents\\\\Git\\\\python-tutorial\\\\01. python tools'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "%pwd" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `mkdir` 产生新文件夹:" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "%mkdir demo_test" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `cd` 改变工作文件夹:" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools\\demo_test\n" - ] - } - ], - "source": [ - "%cd demo_test/" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `writefile` 将cell中的内容写入文件:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing hello_world.py\n" - ] - } - ], - "source": [ - "%%writefile hello_world.py\n", - "print \"hello world\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `ls` 查看当前工作文件夹的文件:" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 驱动器 C 中的卷是 System\n", - " 卷的序列号是 DC4B-D785\n", - "\n", - " C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools\\demo_test 的目录\n", - "\n", - "2015/09/18 11:32 .\n", - "2015/09/18 11:32 ..\n", - "2015/09/18 11:32 19 hello_world.py\n", - " 1 个文件 19 字节\n", - " 2 个目录 121,763,831,808 可用字节\n" - ] - } - ], - "source": [ - "%ls" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `run` 命令来运行这个代码:" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello world\n" - ] - } - ], - "source": [ - "%run hello_world.py" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "删除这个文件:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import os\n", - "os.remove('hello_world.py')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "查看当前文件夹,`hello_world.py` 已被删除:" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 驱动器 C 中的卷是 System\n", - " 卷的序列号是 DC4B-D785\n", - "\n", - " C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools\\demo_test 的目录\n", - "\n", - "2015/09/18 11:32 .\n", - "2015/09/18 11:32 ..\n", - " 0 个文件 0 字节\n", - " 2 个目录 121,763,831,808 可用字节\n" - ] - } - ], - "source": [ - "%ls" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "返回上一层文件夹:" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools\n" - ] - } - ], - "source": [ - "%cd .." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `rmdir` 删除文件夹:" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "%rmdir demo_test" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `hist` 查看历史命令:" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "print \"hello, world\"\n", - "a = 1\n", - "a\n", - "b = [1, 2, 3]\n", - "%lsmagic\n", - "%whos\n", - "%reset -f\n", - "%whos\n", - "%pwd\n", - "%mkdir demo_test\n", - "%cd demo_test/\n", - "%%writefile hello_world.py\n", - "print \"hello world\"\n", - "%ls\n", - "%run hello_world.py\n", - "import os\n", - "os.remove('hello_world.py')\n", - "%ls\n", - "%cd ..\n", - "%rmdir demo_test\n", - "%hist\n" - ] - } - ], - "source": [ - "%hist" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## ipython 使用" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `?` 查看函数的帮助:" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "sum?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `??` 查看函数帮助和函数源代码(如果是用**python**实现的):" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Using matplotlib backend: Qt4Agg\n", - "Populating the interactive namespace from numpy and matplotlib\n" - ] - } - ], - "source": [ - "# 导入numpy和matplotlib两个包\n", - "%pylab\n", - "# 查看其中sort函数的帮助\n", - "sort??" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**ipython**支持使用 `` 键自动补全命令。" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用 `_` 使用上个cell的输出结果:" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "12" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = 12\n", - "a" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "25" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "_ + 13" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以使用 `!` 来执行一些系统命令。" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "正在 Ping baidu.com [180.149.132.47] 具有 32 字节的数据:\n", - "来自 180.149.132.47 的回复: 字节=32 时间=69ms TTL=49\n", - "来自 180.149.132.47 的回复: 字节=32 时间=64ms TTL=49\n", - "来自 180.149.132.47 的回复: 字节=32 时间=61ms TTL=49\n", - "来自 180.149.132.47 的回复: 字节=32 时间=63ms TTL=49\n", - "\n", - "180.149.132.47 的 Ping 统计信息:\n", - " 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),\n", - "往返行程的估计时间(以毫秒为单位):\n", - " 最短 = 61ms,最长 = 69ms,平均 = 64ms\n" - ] - } - ], - "source": [ - "!ping baidu.com" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "当输入出现错误时,**ipython**会指出出错的位置和原因:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "unsupported operand type(s) for +: 'int' and 'str'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m1\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m\"hello\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" - ] - } - ], - "source": [ - "1 + \"hello\"" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Ipython 解释器" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 进入ipython" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "通常我们并不使用**Python**自带的解释器,而是使用另一个比较方便的解释器——**ipython**解释器,命令行下输入:\n", + "\n", + " ipython\n", + "\n", + "即可进入**ipython**解释器。\n", + "\n", + "所有在**python**解释器下可以运行的代码都可以在**ipython**解释器下运行:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello, world\n" + ] + } + ], + "source": [ + "print \"hello, world\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以进行简单赋值操作:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "直接在解释器中输入变量名,会显示变量的值(不需要加`print`):" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "b = [1, 2, 3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ipython magic命令" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**ipython**解释器提供了很多以百分号`%`开头的`magic`命令,这些命令很像linux系统下的命令行命令(事实上有些是一样的)。\n", + "\n", + "查看所有的`magic`命令:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "application/json": { + "cell": { + "!": "OSMagics", + "HTML": "Other", + "SVG": "Other", + "bash": "Other", + "capture": "ExecutionMagics", + "cmd": "Other", + "debug": "ExecutionMagics", + "file": "Other", + "html": "DisplayMagics", + "javascript": "DisplayMagics", + "latex": "DisplayMagics", + "perl": "Other", + "prun": "ExecutionMagics", + "pypy": "Other", + "python": "Other", + "python2": "Other", + "python3": "Other", + "ruby": "Other", + "script": "ScriptMagics", + "sh": "Other", + "svg": "DisplayMagics", + "sx": "OSMagics", + "system": "OSMagics", + "time": "ExecutionMagics", + "timeit": "ExecutionMagics", + "writefile": "OSMagics" + }, + "line": { + "alias": "OSMagics", + "alias_magic": "BasicMagics", + "autocall": "AutoMagics", + "automagic": "AutoMagics", + "autosave": "KernelMagics", + "bookmark": "OSMagics", + "cd": "OSMagics", + "clear": "KernelMagics", + "cls": "KernelMagics", + "colors": "BasicMagics", + "config": "ConfigMagics", + "connect_info": "KernelMagics", + "copy": "Other", + "ddir": "Other", + "debug": "ExecutionMagics", + "dhist": "OSMagics", + "dirs": "OSMagics", + "doctest_mode": "BasicMagics", + "echo": "Other", + "ed": "Other", + "edit": "KernelMagics", + "env": "OSMagics", + "gui": "BasicMagics", + "hist": "Other", + "history": "HistoryMagics", + "install_default_config": "DeprecatedMagics", + "install_ext": "ExtensionMagics", + "install_profiles": "DeprecatedMagics", + "killbgscripts": "ScriptMagics", + "ldir": "Other", + "less": "KernelMagics", + "load": "CodeMagics", + "load_ext": "ExtensionMagics", + "loadpy": "CodeMagics", + "logoff": "LoggingMagics", + "logon": "LoggingMagics", + "logstart": "LoggingMagics", + "logstate": "LoggingMagics", + "logstop": "LoggingMagics", + "ls": "Other", + "lsmagic": "BasicMagics", + "macro": "ExecutionMagics", + "magic": "BasicMagics", + "matplotlib": "PylabMagics", + "mkdir": "Other", + "more": "KernelMagics", + "notebook": "BasicMagics", + "page": "BasicMagics", + "pastebin": "CodeMagics", + "pdb": "ExecutionMagics", + "pdef": "NamespaceMagics", + "pdoc": "NamespaceMagics", + "pfile": "NamespaceMagics", + "pinfo": "NamespaceMagics", + "pinfo2": "NamespaceMagics", + "popd": "OSMagics", + "pprint": "BasicMagics", + "precision": "BasicMagics", + "profile": "BasicMagics", + "prun": "ExecutionMagics", + "psearch": "NamespaceMagics", + "psource": "NamespaceMagics", + "pushd": "OSMagics", + "pwd": "OSMagics", + "pycat": "OSMagics", + "pylab": "PylabMagics", + "qtconsole": "KernelMagics", + "quickref": "BasicMagics", + "recall": "HistoryMagics", + "rehashx": "OSMagics", + "reload_ext": "ExtensionMagics", + "ren": "Other", + "rep": "Other", + "rerun": "HistoryMagics", + "reset": "NamespaceMagics", + "reset_selective": "NamespaceMagics", + "rmdir": "Other", + "run": "ExecutionMagics", + "save": "CodeMagics", + "sc": "OSMagics", + "set_env": "OSMagics", + "store": "StoreMagics", + "sx": "OSMagics", + "system": "OSMagics", + "tb": "ExecutionMagics", + "time": "ExecutionMagics", + "timeit": "ExecutionMagics", + "unalias": "OSMagics", + "unload_ext": "ExtensionMagics", + "who": "NamespaceMagics", + "who_ls": "NamespaceMagics", + "whos": "NamespaceMagics", + "xdel": "NamespaceMagics", + "xmode": "BasicMagics" + } + }, + "text/plain": [ + "Available line magics:\n", + "%alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n", + "\n", + "Available cell magics:\n", + "%%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n", + "\n", + "Automagic is ON, % prefix IS NOT needed for line magics." + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%lsmagic" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`line magic` 以一个百分号开头,作用与一行;\n", + "\n", + "`cell magic` 以两个百分号开头,作用于整个cell。\n", + "\n", + "最后一行`Automagic is ON, % prefix IS NOT needed for line magics.`说明在此时即使不加上`%`也可以使用这些命令。\n", + "\n", + "使用 `whos` 查看当前的变量空间:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Variable Type Data/Info\n", + "----------------------------\n", + "a int 1\n", + "b list n=3\n" + ] + } + ], + "source": [ + "%whos" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `reset` 重置当前变量空间:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%reset -f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "再查看当前变量空间:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Interactive namespace is empty.\n" + ] + } + ], + "source": [ + "%whos" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `pwd` 查看当前工作文件夹:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "u'C:\\\\Users\\\\lijin\\\\Documents\\\\Git\\\\python-tutorial\\\\01. python tools'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%pwd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `mkdir` 产生新文件夹:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%mkdir demo_test" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `cd` 改变工作文件夹:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools\\demo_test\n" + ] + } + ], + "source": [ + "%cd demo_test/" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `writefile` 将cell中的内容写入文件:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Writing hello_world.py\n" + ] + } + ], + "source": [ + "%%writefile hello_world.py\n", + "print \"hello world\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `ls` 查看当前工作文件夹的文件:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 驱动器 C 中的卷是 System\n", + " 卷的序列号是 DC4B-D785\n", + "\n", + " C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools\\demo_test 的目录\n", + "\n", + "2015/09/18 11:32 .\n", + "2015/09/18 11:32 ..\n", + "2015/09/18 11:32 19 hello_world.py\n", + " 1 个文件 19 字节\n", + " 2 个目录 121,763,831,808 可用字节\n" + ] + } + ], + "source": [ + "%ls" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `run` 命令来运行这个代码:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world\n" + ] + } + ], + "source": [ + "%run hello_world.py" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "删除这个文件:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import os\n", + "os.remove('hello_world.py')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "查看当前文件夹,`hello_world.py` 已被删除:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 驱动器 C 中的卷是 System\n", + " 卷的序列号是 DC4B-D785\n", + "\n", + " C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools\\demo_test 的目录\n", + "\n", + "2015/09/18 11:32 .\n", + "2015/09/18 11:32 ..\n", + " 0 个文件 0 字节\n", + " 2 个目录 121,763,831,808 可用字节\n" + ] + } + ], + "source": [ + "%ls" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "返回上一层文件夹:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "C:\\Users\\lijin\\Documents\\Git\\python-tutorial\\01. python tools\n" + ] + } + ], + "source": [ + "%cd .." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `rmdir` 删除文件夹:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%rmdir demo_test" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `hist` 查看历史命令:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "print \"hello, world\"\n", + "a = 1\n", + "a\n", + "b = [1, 2, 3]\n", + "%lsmagic\n", + "%whos\n", + "%reset -f\n", + "%whos\n", + "%pwd\n", + "%mkdir demo_test\n", + "%cd demo_test/\n", + "%%writefile hello_world.py\n", + "print \"hello world\"\n", + "%ls\n", + "%run hello_world.py\n", + "import os\n", + "os.remove('hello_world.py')\n", + "%ls\n", + "%cd ..\n", + "%rmdir demo_test\n", + "%hist\n" + ] + } + ], + "source": [ + "%hist" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ipython 使用" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `?` 查看函数的帮助:" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "sum?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `??` 查看函数帮助和函数源代码(如果是用**python**实现的):" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using matplotlib backend: Qt4Agg\n", + "Populating the interactive namespace from numpy and matplotlib\n" + ] + } + ], + "source": [ + "# 导入numpy和matplotlib两个包\n", + "%pylab\n", + "# 查看其中sort函数的帮助\n", + "sort??" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**ipython** 支持使用 `` 键自动补全命令。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 `_` 使用上个cell的输出结果:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 12\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "25" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "_ + 13" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以使用 `!` 来执行一些系统命令。" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "正在 Ping baidu.com [180.149.132.47] 具有 32 字节的数据:\n", + "来自 180.149.132.47 的回复: 字节=32 时间=69ms TTL=49\n", + "来自 180.149.132.47 的回复: 字节=32 时间=64ms TTL=49\n", + "来自 180.149.132.47 的回复: 字节=32 时间=61ms TTL=49\n", + "来自 180.149.132.47 的回复: 字节=32 时间=63ms TTL=49\n", + "\n", + "180.149.132.47 的 Ping 统计信息:\n", + " 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),\n", + "往返行程的估计时间(以毫秒为单位):\n", + " 最短 = 61ms,最长 = 69ms,平均 = 64ms\n" + ] + } + ], + "source": [ + "!ping baidu.com" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "当输入出现错误时,**ipython**会指出出错的位置和原因:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "unsupported operand type(s) for +: 'int' and 'str'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m1\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m\"hello\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" + ] + } + ], + "source": [ + "1 + \"hello\"" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/02. python essentials/02.01 a tour of python.ipynb b/02. python essentials/02.01 a tour of python.ipynb index dac6cb78..ecdb7610 100644 --- a/02. python essentials/02.01 a tour of python.ipynb +++ b/02. python essentials/02.01 a tour of python.ipynb @@ -1,1935 +1,1935 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Python 入门演示" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##简单的数学运算" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "整数相加,得到整数:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2 + 2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "浮点数相加,得到浮点数:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4.5" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2.0 + 2.5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "整数和浮点数相加,得到浮点数:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4.5" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2 + 2.5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##变量赋值" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python**使用`<变量名>=<表达式>`的方式对变量进行赋值" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "a = 0.2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##字符串 String" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "字符串的生成,单引号与双引号是等价的:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello world'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = \"hello world\"\n", - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello world'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = 'hello world'\n", - "s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "三引号用来输入包含多行文字的字符串:" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello\n", - "world\n" - ] - } - ], - "source": [ - "s = \"\"\"hello\n", - "world\"\"\"\n", - "print s" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello\n", - "world\n" - ] - } - ], - "source": [ - "s = '''hello\n", - "world'''\n", - "print s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "字符串的加法:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello world'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = \"hello\" + \" world\"\n", - "s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "字符串索引:\n" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'h'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s[0]" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'d'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s[-1]" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s[0:5]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "字符串的分割:" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['hello', 'world']" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = \"hello world\"\n", - "s.split()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "查看字符串的长度:" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(s)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##列表 List" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python用`[]`来生成列表" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2.0, 'hello', 6.0]" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = [1, 2.0, 'hello', 5 + 1.0]\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "列表加法:" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2.0, 'hello', 6.0, 1, 2.0, 'hello', 6.0]" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a + a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "列表索引:" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2.0" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "列表长度:" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "向列表中添加元素:" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2.0, 'hello', 6.0, 'world']" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.append(\"world\")\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##集合 Set" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python用{}来生成集合,集合中不含有相同元素。" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{2, 3, 4}" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = {2, 3, 4, 2}\n", - "s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "集合的长度:" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(s)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "向集合中添加元素:" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 2, 3, 4}" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.add(1)\n", - "s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "集合的交:" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{2, 3, 4}" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = {1, 2, 3, 4}\n", - "b = {2, 3, 4, 5}\n", - "a & b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "并:" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 2, 3, 4, 5}" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a | b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "差:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1}" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a - b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "对称差:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 5}" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a ^ b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 字典 Dictionary " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python用`{key:value}`来生成Dictionary。" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'cats': 4, 'dogs': 5}" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d = {'dogs':5, 'cats':4}\n", - "d" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "字典的大小" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(d)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "查看字典某个键对应的值:" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d[\"dogs\"]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "修改键值:" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'cats': 4, 'dogs': 2}" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d[\"dogs\"] = 2\n", - "d" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "插入键值:" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'cats': 4, 'dogs': 2, 'pigs': 7}" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d[\"pigs\"] = 7\n", - "d" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "所有的键:" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['cats', 'dogs', 'pigs']" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.keys()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "所有的值:" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[4, 2, 7]" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.values()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "所有的键值对:" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[('cats', 4), ('dogs', 2), ('pigs', 7)]" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.items()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 数组 Numpy Arrays" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "需要先导入需要的包,Numpy数组可以进行很多列表不能进行的运算。" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([1, 2, 3, 4])" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from numpy import array\n", - "a = array([1, 2, 3, 4])\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "加法:" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([3, 4, 5, 6])" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a + 2" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([2, 4, 6, 8])" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a + a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 画图 Plot" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python提供了一个很像MATLAB的绘图接口。" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXMAAAEACAYAAABBDJb9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEy1JREFUeJzt3X+wXGV5wPHvQ8DRyBRqHYk2dAJMgz8I/qhVhlLZgpnJ\nRIvOWK1MMQRHxhG1alNbsR25jjOlTtv4A3WESiAWSKdVhpEpDUFkMQ4SrQYSJNba0RrtJChSWktb\nk7lP/9i96e269+7u2V/nnP1+ZjKc3T337PvOy33y5nmePRuZiSSp2o6b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MwlqQYM5pJUAwZz\nSaoBg7kk1YDBXJJqwGAuSTVgMJekGvhf3kAwE/Ra4D0AAAAASUVORK5CYII=\n", - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "%matplotlib inline\n", - "from matplotlib.pyplot import plot\n", - "plot(a, a**2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 循环 Loop" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['1', '2', '3', '4', '5']" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "line = '1 2 3 4 5'\n", - "fields = line.split()\n", - "fields" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "total = 0\n", - "for field in fields:\n", - " total += int(field)\n", - "total" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python中有一种叫做列表推导式(List comprehension)的用法:" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5]" - ] - }, - "execution_count": 41, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "numbers = [int(field) for field in fields]\n", - "numbers" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sum(numbers)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "写在一行:" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sum([int(field) for field in line.split()])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 文件操作 File IO" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "d:\\Users\\lijin\n" - ] - } - ], - "source": [ - "cd ~" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "写文件:" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "f = open('data.txt', 'w')\n", - "f.write('1 2 3 4\\n')\n", - "f.write('2 3 4 5\\n')\n", - "f.close()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "读文件:" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[[1, 2, 3, 4], [2, 3, 4, 5]]" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "f = open('data.txt')\n", - "data = []\n", - "for line in f:\n", - " data.append([int(field) for field in line.split()])\n", - "f.close()\n", - "data\n" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4]\n", - "[2, 3, 4, 5]\n" - ] - } - ], - "source": [ - "for row in data:\n", - " print row" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "删除文件:" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "import os\n", - "os.remove('data.txt')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 函数 Function" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python用关键词`def`来定义函数。" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 49, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def poly(x, a, b, c):\n", - " y = a * x ** 2 + b * x + c\n", - " return y\n", - "\n", - "x = 1\n", - "poly(x, 1, 2, 3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "用Numpy数组做参数x:" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 6, 11, 18])" - ] - }, - "execution_count": 50, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = array([1, 2, 3])\n", - "poly(x, 1, 2, 3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以在定义时指定参数的默认值:" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from numpy import arange\n", - "\n", - "def poly(x, a = 1, b = 2, c = 3):\n", - " y = a*x**2 + b*x + c\n", - " return y\n", - "\n", - "x = arange(10)\n", - "x\n", - "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 3, 6, 11, 18, 27, 38, 51, 66, 83, 102])" - ] - }, - "execution_count": 52, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "poly(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 3, 5, 9, 15, 23, 33, 45, 59, 75, 93])" - ] - }, - "execution_count": 53, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "poly(x, b = 1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 模块 Module" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python中使用`import`关键词来导入模块。" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import os" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "当前进程号:" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4400" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "os.getpid()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "系统分隔符:" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'\\\\'" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "os.sep" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## - 类 Class" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "用`class`来定义一个类。\n", - "`Person(object)`表示继承自`object`类;\n", - "`__init__`函数用来初始化对象;\n", - "`self`表示对象自身,类似于`C` `Java`里面`this`。" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class Person(object):\n", - " def __init__(self, first, last, age):\n", - " self.first = first\n", - " self.last = last\n", - " self.age = age\n", - " def full_name(self):\n", - " return self.first + ' ' + self.last" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "构建新对象:" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "person = Person('Mertle', 'Sedgewick', 52)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "调用对象的属性:" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Mertle'" - ] - }, - "execution_count": 59, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "person.first" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "调用对象的方法:" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Mertle Sedgewick'" - ] - }, - "execution_count": 60, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "person.full_name()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "修改对象的属性:" - ] - }, - { - "cell_type": "code", - "execution_count": 61, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "person.last = 'Smith'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "添加新属性,d是之前定义的字典:" - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'cats': 4, 'dogs': 2, 'pigs': 7}" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "person.critters = d\n", - "person.critters" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 网络数据 Data from Web" - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "url = 'http://ichart.finance.yahoo.com/table.csv?s=GE&d=10&e=5&f=2013&g=d&a=0&b=2&c=1962&ignore=.csv'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "处理后就相当于一个可读文件:" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close\\n'],\n", - " ['2013-11-05', '26.32', '26.52', '26.26', '26.42', '24897500', '24.872115\\n'],\n", - " ['2013-11-04',\n", - " '26.59',\n", - " '26.59',\n", - " '26.309999',\n", - " '26.43',\n", - " '28166100',\n", - " '24.88153\\n'],\n", - " ['2013-11-01',\n", - " '26.049999',\n", - " '26.639999',\n", - " '26.030001',\n", - " '26.540001',\n", - " '55634500',\n", - " '24.985086\\n']]" - ] - }, - "execution_count": 64, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import urllib2\n", - "ge_csv = urllib2.urlopen(url)\n", - "data = []\n", - "for line in ge_csv:\n", - " data.append(line.split(','))\n", - "data[:4]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用`pandas`处理数据:" - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWwAAAEECAYAAAAMOA6OAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XeYVdW5+PHvC0PvdagCCqgoBjEaVNRBxaveBDUEFY1o\n9KIx0SDGGCS/6BhjjBqNEvXGHvCqWKNy1asoDkGDEghFYBRBUKSXoUoZhvf3x9qbUzgzp/f38zzz\n7HL23mftNWfeWWftVURVMcYYk/vqZTsBxhhjYmMB2xhj8oQFbGOMyRMWsI0xJk9YwDbGmDxhAdsY\nY/JETAFbROqLyFwRmeJttxWRqSKyRETeFZHW6U2mMcaYWEvYY4DFgN9oexwwVVX7Au9728YYY9Io\nasAWkW7AucATgHi7hwETvfWJwPlpSZ0xxpgDYilh/xn4FbA/aF+pqq7z1tcBpalOmDHGmFB1BmwR\n+T6wXlXnEihdh1DXt936txtjTJqVRHn9JGCYiJ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": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "ge_csv = urllib2.urlopen(url)\n", - "import pandas\n", - "ge = pandas.read_csv(ge_csv, index_col=0, parse_dates=True)\n", - "ge.plot(y='Adj Close')" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.9" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Python 入门演示" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 简单的数学运算" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "整数相加,得到整数:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 + 2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "浮点数相加,得到浮点数:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4.5" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2.0 + 2.5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "整数和浮点数相加,得到浮点数:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4.5" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 + 2.5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 变量赋值" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python**使用`<变量名>=<表达式>`的方式对变量进行赋值" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = 0.2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 字符串 String" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "字符串的生成,单引号与双引号是等价的:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"hello world\"\n", + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = 'hello world'\n", + "s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "三引号用来输入包含多行文字的字符串:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n", + "world\n" + ] + } + ], + "source": [ + "s = \"\"\"hello\n", + "world\"\"\"\n", + "print s" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n", + "world\n" + ] + } + ], + "source": [ + "s = '''hello\n", + "world'''\n", + "print s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "字符串的加法:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"hello\" + \" world\"\n", + "s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "字符串索引:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'h'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'d'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[0:5]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "字符串的分割:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello', 'world']" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"hello world\"\n", + "s.split()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "查看字符串的长度:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 列表 List" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python用`[]`来生成列表" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2.0, 'hello', 6.0]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [1, 2.0, 'hello', 5 + 1.0]\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "列表加法:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2.0, 'hello', 6.0, 1, 2.0, 'hello', 6.0]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a + a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "列表索引:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "列表长度:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "向列表中添加元素:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2.0, 'hello', 6.0, 'world']" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.append(\"world\")\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 集合 Set" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python用{}来生成集合,集合中不含有相同元素。" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{2, 3, 4}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = {2, 3, 4, 2}\n", + "s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "集合的长度:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "向集合中添加元素:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4}" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.add(1)\n", + "s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "集合的交:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{2, 3, 4}" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = {1, 2, 3, 4}\n", + "b = {2, 3, 4, 5}\n", + "a & b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "并:" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 5}" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a | b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "差:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a - b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "对称差:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 5}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a ^ b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 字典 Dictionary " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python用`{key:value}`来生成Dictionary。" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'cats': 4, 'dogs': 5}" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = {'dogs':5, 'cats':4}\n", + "d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "字典的大小" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "查看字典某个键对应的值:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d[\"dogs\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "修改键值:" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'cats': 4, 'dogs': 2}" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d[\"dogs\"] = 2\n", + "d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "插入键值:" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'cats': 4, 'dogs': 2, 'pigs': 7}" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d[\"pigs\"] = 7\n", + "d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "所有的键:" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['cats', 'dogs', 'pigs']" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.keys()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "所有的值:" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 2, 7]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.values()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "所有的键值对:" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('cats', 4), ('dogs', 2), ('pigs', 7)]" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 数组 Numpy Arrays" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "需要先导入需要的包,Numpy数组可以进行很多列表不能进行的运算。" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3, 4])" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from numpy import array\n", + "a = array([1, 2, 3, 4])\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "加法:" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([3, 4, 5, 6])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a + 2" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2, 4, 6, 8])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a + a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 画图 Plot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python提供了一个很像MATLAB的绘图接口。" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXMAAAEACAYAAABBDJb9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEy1JREFUeJzt3X+wXGV5wPHvQ8DRyBRqHYk2dAJMgz8I/qhVhlLZgpnJ\nRIvOWK1MMQRHxhG1alNbsR25jjOlTtv4A3WESiAWSKdVhpEpDUFkMQ4SrQYSJNba0RrtJChSWktb\nk7lP/9i96e269+7u2V/nnP1+ZjKc3T337PvOy33y5nmePRuZiSSp2o6b9gAkScMzmEtSDRjMJakG\nDOaSVAMGc0mqAYO5JNXAssE8IrZFxOGI2N/x/Nsj4kBEPBQRHxzvECVJvfTamd8AbFj8RET8BnAR\ncHZmngX8+ZjGJknq07LBPDN3A491PP0W4OrMPNI+54djGpskqU9Fcua/DLwsIu6PiGZEvHjUg5Ik\nDeb4gj/z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/fYlXu9//TKz8B/g14EXAvuXeH0jcEf7+KXA/cO836T/9DG/\nBvC5aY9ziPmtAl7QPj6RVh3kOXVYwz7nVvX1W9n+7/HA/cB5dVi7AeZX6fVrz+H3gJu7zWPQ9Rtq\nZ56Zu4HHljnlImB7+9w9wMkRccow7zlJfcwPYOCqc1lk5qHMfKB9/BPgAPCsjtMquYZ9zg2qvX5P\ntA+fRKt29eOOUyq5dgv6mB9UeP0iYjWtgP0pus9joPUb9422un24aPWY33OSEji3/U+gOyLiudMe\nUFERsYbWv0L2dLxU+TVcZm6VXr+IOC4iHgAOA/dk5sMdp1R67fqYX6XXD/gQ8G5gfonXB1q/Sdw1\nsfNvnDpVXL8OnJqZzweuAW6b8ngKiYgTgc8A72jvYn/mlI7HlVnDHnOr9Ppl5nxmvoDWL/jLIqLR\n5bTKrl0f86vs+kXEK4FHMnMvy//rou/1G3cw/wFw6qLHq9vP1UJm/sfCPwUz8++BEyLiaVMe1kAi\n4gTgs8BNmdntl6Gya9hrbnVYP4DMfBz4O+DFHS9Vdu0WW2p+FV+/c4GLIuI7wA7ggoj4dMc5A63f\nuIP554BNcOxTo/+WmYfH/J4TExGnRES0j19Cq9WzW16vlNpjvx54ODM/vMRplVzDfuZW5fWLiKdH\nxMnt46cA64G9HadVcu2gv/lVef0y872ZeWpmnga8HvhCZm7qOG2g9Sv0oaEFEbEDOB94ekQcBK4C\nTmgP9trMvCMiNkbEt4H/BC4b5v0mrdf8gN8C3hIRR4EnaC1KlfwacAmwLyIWflHeC/wSVH4Ne86N\naq/fM4HtEXEcrU3ZX2Xm3RHxZqj82kEf86Pa69cpAYZZPz80JEk14NfGSVINGMwlqQYM5pJUAwZz\nSaoBg7kk1YDBXJJqwGAuSTVgMJekGvhf3kAwE/Ra4D0AAAAASUVORK5CYII=\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib inline\n", + "from matplotlib.pyplot import plot\n", + "plot(a, a**2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 循环 Loop" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['1', '2', '3', '4', '5']" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "line = '1 2 3 4 5'\n", + "fields = line.split()\n", + "fields" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total = 0\n", + "for field in fields:\n", + " total += int(field)\n", + "total" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python中有一种叫做列表推导式(List comprehension)的用法:" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers = [int(field) for field in fields]\n", + "numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "写在一行:" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum([int(field) for field in line.split()])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 文件操作 File IO" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "d:\\Users\\lijin\n" + ] + } + ], + "source": [ + "cd ~" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "写文件:" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "f = open('data.txt', 'w')\n", + "f.write('1 2 3 4\\n')\n", + "f.write('2 3 4 5\\n')\n", + "f.close()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "读文件:" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1, 2, 3, 4], [2, 3, 4, 5]]" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = open('data.txt')\n", + "data = []\n", + "for line in f:\n", + " data.append([int(field) for field in line.split()])\n", + "f.close()\n", + "data\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4]\n", + "[2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "for row in data:\n", + " print row" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "删除文件:" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import os\n", + "os.remove('data.txt')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 函数 Function" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python用关键词`def`来定义函数。" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def poly(x, a, b, c):\n", + " y = a * x ** 2 + b * x + c\n", + " return y\n", + "\n", + "x = 1\n", + "poly(x, 1, 2, 3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "用Numpy数组做参数x:" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 6, 11, 18])" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = array([1, 2, 3])\n", + "poly(x, 1, 2, 3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以在定义时指定参数的默认值:" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from numpy import arange\n", + "\n", + "def poly(x, a = 1, b = 2, c = 3):\n", + " y = a*x**2 + b*x + c\n", + " return y\n", + "\n", + "x = arange(10)\n", + "x\n", + "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 3, 6, 11, 18, 27, 38, 51, 66, 83, 102])" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poly(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 3, 5, 9, 15, 23, 33, 45, 59, 75, 93])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poly(x, b = 1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 模块 Module" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python中使用`import`关键词来导入模块。" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "当前进程号:" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4400" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getpid()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "系统分隔符:" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\\\'" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.sep" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## - 类 Class" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "用`class`来定义一个类。\n", + "`Person(object)`表示继承自`object`类;\n", + "`__init__`函数用来初始化对象;\n", + "`self`表示对象自身,类似于`C` `Java`里面`this`。" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Person(object):\n", + " def __init__(self, first, last, age):\n", + " self.first = first\n", + " self.last = last\n", + " self.age = age\n", + " def full_name(self):\n", + " return self.first + ' ' + self.last" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "构建新对象:" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "person = Person('Mertle', 'Sedgewick', 52)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "调用对象的属性:" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Mertle'" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "person.first" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "调用对象的方法:" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Mertle Sedgewick'" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "person.full_name()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "修改对象的属性:" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "person.last = 'Smith'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "添加新属性,d是之前定义的字典:" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'cats': 4, 'dogs': 2, 'pigs': 7}" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "person.critters = d\n", + "person.critters" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 网络数据 Data from Web" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "url = 'http://ichart.finance.yahoo.com/table.csv?s=GE&d=10&e=5&f=2013&g=d&a=0&b=2&c=1962&ignore=.csv'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "处理后就相当于一个可读文件:" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close\\n'],\n", + " ['2013-11-05', '26.32', '26.52', '26.26', '26.42', '24897500', '24.872115\\n'],\n", + " ['2013-11-04',\n", + " '26.59',\n", + " '26.59',\n", + " '26.309999',\n", + " '26.43',\n", + " '28166100',\n", + " '24.88153\\n'],\n", + " ['2013-11-01',\n", + " '26.049999',\n", + " '26.639999',\n", + " '26.030001',\n", + " '26.540001',\n", + " '55634500',\n", + " '24.985086\\n']]" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import urllib2\n", + "ge_csv = urllib2.urlopen(url)\n", + "data = []\n", + "for line in ge_csv:\n", + " data.append(line.split(','))\n", + "data[:4]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用`pandas`处理数据:" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWwAAAEECAYAAAAMOA6OAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XeYVdW5+PHvC0PvdagCCqgoBjEaVNRBxaveBDUEFY1o\n9KIx0SDGGCS/6BhjjBqNEvXGHvCqWKNy1asoDkGDEghFYBRBUKSXoUoZhvf3x9qbUzgzp/f38zzz\n7HL23mftNWfeWWftVURVMcYYk/vqZTsBxhhjYmMB2xhj8oQFbGOMyRMWsI0xJk9YwDbGmDxhAdsY\nY/JETAFbROqLyFwRmeJttxWRqSKyRETeFZHW6U2mMcaYWEvYY4DFgN9oexwwVVX7Au9728YYY9Io\nasAWkW7AucATgHi7hwETvfWJwPlpSZ0xxpgDYilh/xn4FbA/aF+pqq7z1tcBpalOmDHGmFB1BmwR\n+T6wXlXnEihdh1DXt936txtjTJqVRHn9JGCYiJwLNAZaisgzwDoR6aSqa0WkM7A+0skiYoHcGGMS\noKoHFZLrLGGr6nhV7a6qvYCLgWmqehnwBnC5d9jlwGt1XKMgfm677baspyHbP5YHlgfFfv+ZyoPa\nxNsO27/SH4GhIrIEON3bNsYYk0bRqkQOUNXpwHRvfTNwZroSZYwx5mDW0zFGZWVl2U5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EuAqVV2T\nzfRlQrHnQTHfv9fWXHGtYK5X1aVee+JNwFHAClX9pq5r5LtCzoOCCtgibmQ1EXkF14NpL+5h2heq\nujS7qcuMYs+DYr9/AHFjpDyO63p9Fa6Ueb2qbstqwjKoUPOgYJr1gXvCLyJNgY64jiB3qOrbWU5W\nRhV7HhT7/XuOxdXf9gKeVtUns5yebCjIPCiogO25Flc/dabmYDvKDCn2PCj2+1+JqwL6k7refMWo\nIPOgoKpEwHWe0CKcLSRYsedBsd+/KVwFF7CNMaZQFUSzPmOMKQYWsI0xJk9YwDbGmDxhAdsYY/KE\nBWxTMESkRkTmishCEZknIjd60z7VdU4PERmZqTQakwwL2KaQfKuqx6rq0bhxJM4BbotyTi/cTCTG\n5DwL2KYgqZuM4mrgOgAR6Ski/xCROd7Pid6hfwRO8UrmY0SknojcKyKzRGS+iFydrXswJpy1wzYF\nQ0S2q2qLsH1VQF/c5MP7VXWPiPQBnlPV471B729S1R94x18NdFDVO0WkEfAhMEJVV2T0ZoyJoBC7\nphsTSUPcJLTfwQ2v2sfbH17HfRbQX0R+5G23xE32sCITiTSmLhawTcESkUOBGlXdICLlwBpVvcyb\n9293Hadep24KOWNyitVhm4IkIh1wc1X+xdvVEljrrY/CzUYDsB03UavvHeBn3pjKiEhfb/Q/Y7LO\nStimkDQRkbm4SZf34aZD8yfdfQR4RURGAf+Hq9MGmA/UiMg84GlgAtAT+LfXJHA9bnZxY7LOHjoa\nY0yesCoRY4zJExawjTEmT1jANsaYPGEB2xhj8oQFbGOMyRMWsI0xJk9YwDbGmDxhAdsYY/LE/we1\nG8sUQyi3yAAAAABJRU5ErkJggg==\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "ge_csv = urllib2.urlopen(url)\n", + "import pandas\n", + "ge = pandas.read_csv(ge_csv, index_col=0, parse_dates=True)\n", + "ge.plot(y='Adj Close')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/02. python essentials/02.02 python data types.ipynb b/02. python essentials/02.02 python data types.ipynb index 86d73a0b..1407fc22 100644 --- a/02. python essentials/02.02 python data types.ipynb +++ b/02. python essentials/02.02 python data types.ipynb @@ -1,74 +1,74 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Python 数据类型" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##常用数据类型 Common Data Types" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "| 类型| 例子|\n", - "| ----- | ----- |\n", - "| 整数 | `-100` |\n", - "| 浮点数 | `3.1416` |\n", - "| 字符串 | `'hello'` |\n", - "| 列表 | `[1, 1.2, 'hello']` |\n", - "| 字典 | `{'dogs': 5, 'pigs': 3}`|\n", - "| Numpy数组 | `array([1, 2, 3])`" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 其他类型 Others" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "| 类型| 例子|\n", - "| ------- | ----- |\n", - "| 长整型 | `1000000000000L`\n", - "| 布尔型 | `True, False`\n", - "| 元组 | `('ring', 1000)`\n", - "| 集合 | `{1, 2, 3}`\n", - "| Pandas类型| `DataFrame, Series`\n", - "| 自定义 | `Object Oriented Classes`" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.9" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Python 数据类型" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 常用数据类型 Common Data Types" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "| 类型| 例子|\n", + "| ----- | ----- |\n", + "| 整数 | `-100` |\n", + "| 浮点数 | `3.1416` |\n", + "| 字符串 | `'hello'` |\n", + "| 列表 | `[1, 1.2, 'hello']` |\n", + "| 字典 | `{'dogs': 5, 'pigs': 3}`|\n", + "| Numpy数组 | `array([1, 2, 3])`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 其他类型 Others" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "| 类型| 例子|\n", + "| ------- | ----- |\n", + "| 长整型 | `1000000000000L`\n", + "| 布尔型 | `True, False`\n", + "| 元组 | `('ring', 1000)`\n", + "| 集合 | `{1, 2, 3}`\n", + "| Pandas类型| `DataFrame, Series`\n", + "| 自定义 | `Object Oriented Classes`" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/02. python essentials/02.03 numbers.ipynb b/02. python essentials/02.03 numbers.ipynb index 2e4461ed..7c80e8da 100644 --- a/02. python essentials/02.03 numbers.ipynb +++ b/02. python essentials/02.03 numbers.ipynb @@ -1,1546 +1,1546 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 数字" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 整型 Integers" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "整型运算,加减乘:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2 + 2" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "-1" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "3 - 4" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "20" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "4 * 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "在**Python 2.7**中,整型的运算结果只能返回整型,**除法**的结果也不例外。\n", - "\n", - "例如`12 / 5`返回的结果并不是2.4,而是2:" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "12 / 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "幂指数:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "32" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2 ** 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "取余:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "32 % 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "赋值给变量:" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = 1\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "使用`type()`函数来查看变量类型:" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "整型数字的最大最小值:\n", - "\n", - "在 32 位系统中,一个整型 4 个字节,最小值 `-2,147,483,648`,最大值 `2,147,483,647`。\n", - "\n", - "在 64 位系统中,一个整型 8 个字节,最小值 `-9,223,372,036,854,775,808`,最大值 `9,223,372,036,854,775,807`。" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2147483647" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import sys\n", - "sys.maxint" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##长整型 Long Integers" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "当整型超出范围时,**Python**会自动将整型转化为长整型,不过长整型计算速度会比整型慢。" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "a = sys.maxint + 1\n", - "print type(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "长整型的一个标志是后面以字母L结尾:" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2147483648L" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以在赋值时强制让类型为长整型:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "long" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "b = 1234L\n", - "type(b)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "长整型可以与整型在一起进行计算,返回的类型还是长整型:" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2147483644L" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a - 4" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 浮点数 Floating Point Numbers" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "float" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = 1.4\n", - "type(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "在之前的除法例子`12 / 5`中,假如想要使返回的结果为2.4,可以将它们写成浮点数的形式:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2.4" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "12.0 / 5.0" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2.4" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "12 / 5.0" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2.4" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "12.0 / 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "上面的例子说明,浮点数与整数进行运算时,返回的仍然是浮点数:" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "7.4" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "5 + 2.4" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "浮点数也可以进行与整数相似的运算,甚至可以取余:" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0.19999999999999973" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "3.4 - 3.2" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "44.7" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "12.3 + 32.4" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "6.25" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2.5 ** 2" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1.2999999999999998" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "3.4 % 2.1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python**的浮点数标准与**C**,**Java**一致,都是[IEEE 754 floating point standard](http://en.wikipedia.org/wiki/IEEE_floating_point)。\n", - "\n", - "注意看 `3.4 - 3.2` 的结果并不是我们预期的`0.2`,这是因为浮点数本身储存方式引起的,浮点数本身会存在一点误差。\n", - "\n", - "事实上,**Python** 中储存的值为'0.199999999999999733546474089962430298328399658203125',因为这是最接近0.2的浮点数。|" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0.199999999999999733546474089962430298328399658203125'" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'{:.52}'.format(3.4 - 3.2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "当我们使用`print`显示时,**Python**会自动校正这个结果" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0.2\n" - ] - } - ], - "source": [ - "print 3.4 - 3.2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以用`sys.float_info`来查看浮点数的信息:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import sys\n", - "sys.float_info" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "例如浮点数能表示的最大值:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1.7976931348623157e+308" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sys.float_info.max" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "浮点数能表示的最接近0的值:" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2.2250738585072014e-308" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sys.float_info.min" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "浮点数的精度:" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2.220446049250313e-16" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sys.float_info.epsilon" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##复数 Complex Numbers" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python**使用`j`来表示复数的虚部:" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "complex" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = 1 + 2j\n", - "type(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以查看它的实部,虚部以及共轭:" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1.0" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.real" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2.0" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.imag" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(1-2j)" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.conjugate()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##交互计算" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以将复杂的表达式放在一起计算:" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "-27" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1 + 2 - (3 * 4 / 6) ** 5 + 7 % 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "在**Python**中运算是有优先级的,优先级即算术的先后顺序,比如“先乘除后加减”和“先算括号里面的”都是两种优先级的规则,优先级从高到低排列如下:\n", - "\n", - "- `( )` 括号\n", - "- `**` 幂指数运算\n", - "- `* / // %` 乘,除,整数除法,取余运算\n", - "- '+ -' 加减\n", - "\n", - "整数除法,返回的是比结果小的最大整数值:" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2.0" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "12.3 // 5.2" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "-4.0" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "12.3 // -4" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##简单的数学函数" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "绝对值:" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "12.4" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "abs(-12.4)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "取整:" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "22.0" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "round(21.6)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "最大最小值:" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "4\n" - ] - } - ], - "source": [ - "print min(2, 3, 4, 5)\n", - "print max(2, 4, 3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##变量名覆盖" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "不要用内置的函数来命名变量,否则会出现意想不到的结果:" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "builtin_function_or_method" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(max)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "不要这样做!!!" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "max = 1\n", - "type(max)" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'int' object is not callable", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmax\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: 'int' object is not callable" - ] - } - ], - "source": [ - "max(4, 5)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##类型转换" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "浮点数转整型,只保留整数部分:" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "12\n", - "-3\n" - ] - } - ], - "source": [ - "print int(12.324)\n", - "print int(-3.32)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "整型转浮点型:" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.2\n" - ] - } - ], - "source": [ - "print float(1.2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##其他表示" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "除了10进制外,整数还有其他类型的表示方法。" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "科学计数法:" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1e-06" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1e-6" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "16进制,前面加`0x`修饰,后面使用数字0-9A-F:" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "255" - ] - }, - "execution_count": 45, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "0xFF" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "8进制,前面加`0`或者`0o`修饰,后面使用数字0-7:" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "55" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "067" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "2进制,前面加`0b`修饰,后面使用数字0或1:" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "42" - ] - }, - "execution_count": 47, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "0b101010" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##原地计算 In-place" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python**可以使用下面的形式进行原地计算:" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4.5\n", - "9.0\n", - "6.0\n" - ] - } - ], - "source": [ - "b = 2.5\n", - "b += 2\n", - "print b\n", - "b *= 2\n", - "print b\n", - "b -= 3\n", - "print b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##布尔型 Boolean Data Type" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "布尔型可以看成特殊的二值变量,其取值为`True`和`False`:" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "bool" - ] - }, - "execution_count": 49, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q = True\n", - "type(q)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以用表达式构建布尔型变量:" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n" - ] - } - ], - "source": [ - "q = 1 > 2\n", - "print q" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "常用的比较符号包括:\n", - "\n", - " <, >, <=, >=, ==, !=\n", - "\n", - "**Python**支持链式比较:" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = 2 \n", - "1 < x <= 3" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 数字" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 整型 Integers" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "整型运算,加减乘:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 + 2" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3 - 4" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "4 * 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在**Python 2.7**中,整型的运算结果只能返回整型,**除法**的结果也不例外。\n", + "\n", + "例如`12 / 5`返回的结果并不是2.4,而是2:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "12 / 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "幂指数:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "32" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 ** 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "取余:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "32 % 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "赋值给变量:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 1\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用`type()`函数来查看变量类型:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "整型数字的最大最小值:\n", + "\n", + "在 32 位系统中,一个整型 4 个字节,最小值 `-2,147,483,648`,最大值 `2,147,483,647`。\n", + "\n", + "在 64 位系统中,一个整型 8 个字节,最小值 `-9,223,372,036,854,775,808`,最大值 `9,223,372,036,854,775,807`。" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2147483647" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import sys\n", + "sys.maxint" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 长整型 Long Integers" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "当整型超出范围时,**Python**会自动将整型转化为长整型,不过长整型计算速度会比整型慢。" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "a = sys.maxint + 1\n", + "print type(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "长整型的一个标志是后面以字母L结尾:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2147483648L" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以在赋值时强制让类型为长整型:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "long" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = 1234L\n", + "type(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "长整型可以与整型在一起进行计算,返回的类型还是长整型:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2147483644L" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a - 4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 浮点数 Floating Point Numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 1.4\n", + "type(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在之前的除法例子`12 / 5`中,假如想要使返回的结果为2.4,可以将它们写成浮点数的形式:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.4" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "12.0 / 5.0" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.4" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "12 / 5.0" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.4" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "12.0 / 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "上面的例子说明,浮点数与整数进行运算时,返回的仍然是浮点数:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "7.4" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 + 2.4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "浮点数也可以进行与整数相似的运算,甚至可以取余:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.19999999999999973" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3.4 - 3.2" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "44.7" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "12.3 + 32.4" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "6.25" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2.5 ** 2" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1.2999999999999998" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3.4 % 2.1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python**的浮点数标准与**C**,**Java**一致,都是[IEEE 754 floating point standard](http://en.wikipedia.org/wiki/IEEE_floating_point)。\n", + "\n", + "注意看 `3.4 - 3.2` 的结果并不是我们预期的`0.2`,这是因为浮点数本身储存方式引起的,浮点数本身会存在一点误差。\n", + "\n", + "事实上,**Python** 中储存的值为'0.199999999999999733546474089962430298328399658203125',因为这是最接近0.2的浮点数。|" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'0.199999999999999733546474089962430298328399658203125'" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'{:.52}'.format(3.4 - 3.2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "当我们使用`print`显示时,**Python**会自动校正这个结果" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.2\n" + ] + } + ], + "source": [ + "print 3.4 - 3.2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以用`sys.float_info`来查看浮点数的信息:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import sys\n", + "sys.float_info" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "例如浮点数能表示的最大值:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1.7976931348623157e+308" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sys.float_info.max" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "浮点数能表示的最接近0的值:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.2250738585072014e-308" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sys.float_info.min" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "浮点数的精度:" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.220446049250313e-16" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sys.float_info.epsilon" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 复数 Complex Numbers" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python** 使用 `j` 来表示复数的虚部:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "complex" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 1 + 2j\n", + "type(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以查看它的实部,虚部以及共轭:" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.real" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.imag" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(1-2j)" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.conjugate()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##交互计算" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以将复杂的表达式放在一起计算:" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "-27" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 + 2 - (3 * 4 / 6) ** 5 + 7 % 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在**Python**中运算是有优先级的,优先级即算术的先后顺序,比如“先乘除后加减”和“先算括号里面的”都是两种优先级的规则,优先级从高到低排列如下:\n", + "\n", + "- `( )` 括号\n", + "- `**` 幂指数运算\n", + "- `* / // %` 乘,除,整数除法,取余运算\n", + "- '+ -' 加减\n", + "\n", + "整数除法,返回的是比结果小的最大整数值:" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "12.3 // 5.2" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "-4.0" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "12.3 // -4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 简单的数学函数" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "绝对值:" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12.4" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "abs(-12.4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "取整:" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "22.0" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "round(21.6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "最大最小值:" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n" + ] + } + ], + "source": [ + "print min(2, 3, 4, 5)\n", + "print max(2, 4, 3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 变量名覆盖" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "不要用内置的函数来命名变量,否则会出现意想不到的结果:" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "builtin_function_or_method" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(max)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "不要这样做!!!" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max = 1\n", + "type(max)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'int' object is not callable", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmax\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'int' object is not callable" + ] + } + ], + "source": [ + "max(4, 5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 类型转换" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "浮点数转整型,只保留整数部分:" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12\n", + "-3\n" + ] + } + ], + "source": [ + "print int(12.324)\n", + "print int(-3.32)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "整型转浮点型:" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.2\n" + ] + } + ], + "source": [ + "print float(1.2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 其他表示" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "除了10进制外,整数还有其他类型的表示方法。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "科学计数法:" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1e-06" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1e-6" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "16进制,前面加`0x`修饰,后面使用数字0-9A-F:" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "255" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "0xFF" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "8进制,前面加`0`或者`0o`修饰,后面使用数字0-7:" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "067" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2进制,前面加`0b`修饰,后面使用数字0或1:" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "42" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "0b101010" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 原地计算 In-place" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python**可以使用下面的形式进行原地计算:" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4.5\n", + "9.0\n", + "6.0\n" + ] + } + ], + "source": [ + "b = 2.5\n", + "b += 2\n", + "print b\n", + "b *= 2\n", + "print b\n", + "b -= 3\n", + "print b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 布尔型 Boolean Data Type" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "布尔型可以看成特殊的二值变量,其取值为`True`和`False`:" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bool" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q = True\n", + "type(q)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以用表达式构建布尔型变量:" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "q = 1 > 2\n", + "print q" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "常用的比较符号包括:\n", + "\n", + " <, >, <=, >=, ==, !=\n", + "\n", + "**Python**支持链式比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = 2 \n", + "1 < x <= 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/02. python essentials/02.04 strings.ipynb b/02. python essentials/02.04 strings.ipynb index b8fd20ef..ab859a1d 100644 --- a/02. python essentials/02.04 strings.ipynb +++ b/02. python essentials/02.04 strings.ipynb @@ -1,1306 +1,1306 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 字符串" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 生成字符串" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python**中可以使用一对单引号''或者双引号\"\"生成字符串。" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello, world\n" - ] - } - ], - "source": [ - "s = \"hello, world\"\n", - "print s" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello world\n" - ] - } - ], - "source": [ - "s = 'hello world'\n", - "print s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 简单操作" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "加法:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello world'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = 'hello ' + 'world'\n", - "s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "字符串与数字相乘:" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'echoechoecho'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "\"echo\" * 3" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "字符串长度:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(s)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 字符串方法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python**是一种面向对象的语言,面向对象的语言中一个必不可少的元素就是方法,而字符串是对象的一种,所以有很多可用的方法。\n", - "\n", - "跟很多语言一样,**Python**使用以下形式来调用方法:\n", - "\n", - " 对象.方法(参数)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 分割" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "s.split()将s按照空格(包括多个空格,制表符`\\t`,换行符`\\n`等)分割,并返回所有分割得到的字符串。" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['1', '2', '3', '4', '5']\n" - ] - } - ], - "source": [ - "line = \"1 2 3 4 5\"\n", - "numbers = line.split()\n", - "print numbers" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "s.split(sep)以给定的sep为分隔符对s进行分割。" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['1', '2', '3', '4', '5']\n" - ] - } - ], - "source": [ - "line = \"1,2,3,4,5\"\n", - "numbers = line.split(',')\n", - "print numbers" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 连接" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "与分割相反,s.join(str_sequence)的作用是以s为连接符将字符串序列str_sequence中的元素连接起来,并返回连接后得到的新字符串:" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'1 2 3 4 5'" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = ' '\n", - "s.join(numbers)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'1,2,3,4,5'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = ','\n", - "s.join(numbers)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 替换" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "s.replace(part1, part2)将字符串s中指定的部分part1替换成想要的部分part2,并返回新的字符串。" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello python'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = \"hello world\"\n", - "s.replace('world', 'python')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "此时,s的值并没有变化,替换方法只是生成了一个新的字符串。" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello world'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 大小写转换" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "s.upper()方法返回一个将s中的字母全部大写的新字符串。\n", - "\n", - "s.lower()方法返回一个将s中的字母全部小写的新字符串。" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'HELLO WORLD'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "\"hello world\".upper()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "这两种方法也不会改变原来s的值:" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello world\n", - "HELLO WORLD\n" - ] - } - ], - "source": [ - "s = \"HELLO WORLD\"\n", - "print s.lower()\n", - "print s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 去除多余空格" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "s.strip()返回一个将s两端的多余空格除去的新字符串。\n", - "\n", - "s.lstrip()返回一个将s开头的多余空格除去的新字符串。\n", - "\n", - "s.rstrip()返回一个将s结尾的多余空格除去的新字符串。" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello world'" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s = \" hello world \"\n", - "s.strip()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "s的值依然不会变化:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' hello world '" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello world '" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.lstrip()" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' hello world'" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.rstrip()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##更多方法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以使用dir函数查看所有可以使用的方法:" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__add__',\n", - " '__class__',\n", - " '__contains__',\n", - " '__delattr__',\n", - " '__doc__',\n", - " '__eq__',\n", - " '__format__',\n", - " '__ge__',\n", - " '__getattribute__',\n", - " '__getitem__',\n", - " '__getnewargs__',\n", - " '__getslice__',\n", - " '__gt__',\n", - " '__hash__',\n", - " '__init__',\n", - " '__le__',\n", - " '__len__',\n", - " '__lt__',\n", - " '__mod__',\n", - " '__mul__',\n", - " '__ne__',\n", - " '__new__',\n", - " '__reduce__',\n", - " '__reduce_ex__',\n", - " '__repr__',\n", - " '__rmod__',\n", - " '__rmul__',\n", - " '__setattr__',\n", - " '__sizeof__',\n", - " '__str__',\n", - " '__subclasshook__',\n", - " '_formatter_field_name_split',\n", - " '_formatter_parser',\n", - " 'capitalize',\n", - " 'center',\n", - " 'count',\n", - " 'decode',\n", - " 'encode',\n", - " 'endswith',\n", - " 'expandtabs',\n", - " 'find',\n", - " 'format',\n", - " 'index',\n", - " 'isalnum',\n", - " 'isalpha',\n", - " 'isdigit',\n", - " 'islower',\n", - " 'isspace',\n", - " 'istitle',\n", - " 'isupper',\n", - " 'join',\n", - " 'ljust',\n", - " 'lower',\n", - " 'lstrip',\n", - " 'partition',\n", - " 'replace',\n", - " 'rfind',\n", - " 'rindex',\n", - " 'rjust',\n", - " 'rpartition',\n", - " 'rsplit',\n", - " 'rstrip',\n", - " 'split',\n", - " 'splitlines',\n", - " 'startswith',\n", - " 'strip',\n", - " 'swapcase',\n", - " 'title',\n", - " 'translate',\n", - " 'upper',\n", - " 'zfill']" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(s)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 多行字符串" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python用一对\"\"\"或者'''来生成多行字符串:" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello world.\n", - "it is a nice day.\n" - ] - } - ], - "source": [ - "a = \"\"\"hello world.\n", - "it is a nice day.\"\"\"\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "在储存时,我们在两行字符间加上一个换行符'\\n'" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello world.\\nit is a nice day.'" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "##使用 `()` 或者 `\\` 来换行" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "当代码太长或者为了美观起见时,我们可以使用两种方法来将一行代码转为多行代码:\n", - "\n", - "* ()\n", - "* \\" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "\"hello, world. it's a nice day. my name is xxx\"" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = (\"hello, world. \"\n", - " \"it's a nice day. \"\n", - " \"my name is xxx\")\n", - "a" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "\"hello, world. it's a nice day. my name is xxx\"" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = \"hello, world. \" \\\n", - " \"it's a nice day. \" \\\n", - " \"my name is xxx\"\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 强制转换为字符串" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* `str(ob)`强制将`ob`转化成字符串。 \n", - "* `repr(ob)`也是强制将`ob`转化成字符串。\n", - "\n", - "不同点如下:" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'3.3'" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "str(1.1 + 2.2)" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'3.3000000000000003'" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "repr(1.1 + 2.2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 整数与不同进制的字符串的转化" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以将整数按照不同进制转化为不同类型的字符串。\n", - "\n", - "十六进制:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0xff'" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "hex(255)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "八进制:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0377'" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "oct(255)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "二进制:" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0b11111111'" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bin(255)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以使用 `int` 将字符串转为整数:" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "23" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "int('23')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "还可以指定按照多少进制来进行转换,最后返回十进制表达的整数:" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "255" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "int('FF', 16)" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "255" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "int('377', 8)" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "255" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "int('11111111', 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`float` 可以将字符串转换为浮点数:" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3.5" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "float('3.5')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 格式化字符串" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python**用字符串的`format()`方法来格式化字符串。\n", - "\n", - "具体用法如下,字符串中花括号 `{}` 的部分会被format传入的参数替代,传入的值可以是字符串,也可以是数字或者别的对象。" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'a b c'" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'{} {} {}'.format('a', 'b', 'c')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以用数字指定传入参数的相对位置:" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'c b a'" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'{2} {1} {0}'.format('a', 'b', 'c')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "还可以指定传入参数的名称:" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'blue 10 1.5'" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'{color} {n} {x}'.format(n=10, x=1.5, color='blue')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以在一起混用:" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'blue 10 1.5 foo'" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'{color} {0} {x} {1}'.format(10, 'foo', x = 1.5, color='blue')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以用`{:}`指定格式:" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'foo 5 6.28'" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from math import pi\n", - "\n", - "'{0:10} {1:10d} {2:10.2f}'.format('foo', 5, 2 * pi)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "具体规则与C中相同。" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "也可以使用旧式的 `%` 方法进行格式化:" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = \"some numbers:\"\n", - "x = 1.34\n", - "y = 2\n", - "# 用百分号隔开,括号括起来\n", - "t = \"%s %f, %d\" % (s, x, y)" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'some numbers: 1.340000, 2'" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "t" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 字符串" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 生成字符串" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python**中可以使用一对单引号''或者双引号\"\"生成字符串。" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello, world\n" + ] + } + ], + "source": [ + "s = \"hello, world\"\n", + "print s" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world\n" + ] + } + ], + "source": [ + "s = 'hello world'\n", + "print s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 简单操作" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "加法:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = 'hello ' + 'world'\n", + "s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "字符串与数字相乘:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'echoechoecho'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"echo\" * 3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "字符串长度:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 字符串方法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python**是一种面向对象的语言,面向对象的语言中一个必不可少的元素就是方法,而字符串是对象的一种,所以有很多可用的方法。\n", + "\n", + "跟很多语言一样,**Python**使用以下形式来调用方法:\n", + "\n", + " 对象.方法(参数)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 分割" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "s.split()将s按照空格(包括多个空格,制表符`\\t`,换行符`\\n`等)分割,并返回所有分割得到的字符串。" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['1', '2', '3', '4', '5']\n" + ] + } + ], + "source": [ + "line = \"1 2 3 4 5\"\n", + "numbers = line.split()\n", + "print numbers" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "s.split(sep)以给定的sep为分隔符对s进行分割。" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['1', '2', '3', '4', '5']\n" + ] + } + ], + "source": [ + "line = \"1,2,3,4,5\"\n", + "numbers = line.split(',')\n", + "print numbers" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 连接" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "与分割相反,s.join(str_sequence)的作用是以s为连接符将字符串序列str_sequence中的元素连接起来,并返回连接后得到的新字符串:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'1 2 3 4 5'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = ' '\n", + "s.join(numbers)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'1,2,3,4,5'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = ','\n", + "s.join(numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 替换" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "s.replace(part1, part2)将字符串s中指定的部分part1替换成想要的部分part2,并返回新的字符串。" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello python'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"hello world\"\n", + "s.replace('world', 'python')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "此时,s的值并没有变化,替换方法只是生成了一个新的字符串。" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 大小写转换" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "s.upper()方法返回一个将s中的字母全部大写的新字符串。\n", + "\n", + "s.lower()方法返回一个将s中的字母全部小写的新字符串。" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'HELLO WORLD'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"hello world\".upper()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "这两种方法也不会改变原来s的值:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world\n", + "HELLO WORLD\n" + ] + } + ], + "source": [ + "s = \"HELLO WORLD\"\n", + "print s.lower()\n", + "print s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 去除多余空格" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "s.strip()返回一个将s两端的多余空格除去的新字符串。\n", + "\n", + "s.lstrip()返回一个将s开头的多余空格除去的新字符串。\n", + "\n", + "s.rstrip()返回一个将s结尾的多余空格除去的新字符串。" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world'" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \" hello world \"\n", + "s.strip()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "s的值依然不会变化:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "' hello world '" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world '" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.lstrip()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "' hello world'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.rstrip()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 更多方法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以使用dir函数查看所有可以使用的方法:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['__add__',\n", + " '__class__',\n", + " '__contains__',\n", + " '__delattr__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__getitem__',\n", + " '__getnewargs__',\n", + " '__getslice__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__le__',\n", + " '__len__',\n", + " '__lt__',\n", + " '__mod__',\n", + " '__mul__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__rmod__',\n", + " '__rmul__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '_formatter_field_name_split',\n", + " '_formatter_parser',\n", + " 'capitalize',\n", + " 'center',\n", + " 'count',\n", + " 'decode',\n", + " 'encode',\n", + " 'endswith',\n", + " 'expandtabs',\n", + " 'find',\n", + " 'format',\n", + " 'index',\n", + " 'isalnum',\n", + " 'isalpha',\n", + " 'isdigit',\n", + " 'islower',\n", + " 'isspace',\n", + " 'istitle',\n", + " 'isupper',\n", + " 'join',\n", + " 'ljust',\n", + " 'lower',\n", + " 'lstrip',\n", + " 'partition',\n", + " 'replace',\n", + " 'rfind',\n", + " 'rindex',\n", + " 'rjust',\n", + " 'rpartition',\n", + " 'rsplit',\n", + " 'rstrip',\n", + " 'split',\n", + " 'splitlines',\n", + " 'startswith',\n", + " 'strip',\n", + " 'swapcase',\n", + " 'title',\n", + " 'translate',\n", + " 'upper',\n", + " 'zfill']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 多行字符串" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python 用一对 `\"\"\"` 或者 `'''` 来生成多行字符串:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world.\n", + "it is a nice day.\n" + ] + } + ], + "source": [ + "a = \"\"\"hello world.\n", + "it is a nice day.\"\"\"\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在储存时,我们在两行字符间加上一个换行符 `'\\n'`" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world.\\nit is a nice day.'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "## 使用 `()` 或者 `\\` 来换行" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "当代码太长或者为了美观起见时,我们可以使用两种方法来将一行代码转为多行代码:\n", + "\n", + "* ()\n", + "* \\" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\"hello, world. it's a nice day. my name is xxx\"" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = (\"hello, world. \"\n", + " \"it's a nice day. \"\n", + " \"my name is xxx\")\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\"hello, world. it's a nice day. my name is xxx\"" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = \"hello, world. \" \\\n", + " \"it's a nice day. \" \\\n", + " \"my name is xxx\"\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 强制转换为字符串" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* `str(ob)`强制将`ob`转化成字符串。 \n", + "* `repr(ob)`也是强制将`ob`转化成字符串。\n", + "\n", + "不同点如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'3.3'" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str(1.1 + 2.2)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'3.3000000000000003'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "repr(1.1 + 2.2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 整数与不同进制的字符串的转化" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以将整数按照不同进制转化为不同类型的字符串。\n", + "\n", + "十六进制:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'0xff'" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hex(255)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "八进制:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'0377'" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "oct(255)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "二进制:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'0b11111111'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bin(255)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以使用 `int` 将字符串转为整数:" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "23" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int('23')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "还可以指定按照多少进制来进行转换,最后返回十进制表达的整数:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "255" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int('FF', 16)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "255" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int('377', 8)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "255" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int('11111111', 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`float` 可以将字符串转换为浮点数:" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3.5" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "float('3.5')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 格式化字符串" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python**用字符串的`format()`方法来格式化字符串。\n", + "\n", + "具体用法如下,字符串中花括号 `{}` 的部分会被format传入的参数替代,传入的值可以是字符串,也可以是数字或者别的对象。" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'a b c'" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'{} {} {}'.format('a', 'b', 'c')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以用数字指定传入参数的相对位置:" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'c b a'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'{2} {1} {0}'.format('a', 'b', 'c')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "还可以指定传入参数的名称:" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'blue 10 1.5'" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'{color} {n} {x}'.format(n=10, x=1.5, color='blue')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以在一起混用:" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'blue 10 1.5 foo'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'{color} {0} {x} {1}'.format(10, 'foo', x = 1.5, color='blue')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以用`{:}`指定格式:" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'foo 5 6.28'" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from math import pi\n", + "\n", + "'{0:10} {1:10d} {2:10.2f}'.format('foo', 5, 2 * pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "具体规则与C中相同。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "也可以使用旧式的 `%` 方法进行格式化:" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "s = \"some numbers:\"\n", + "x = 1.34\n", + "y = 2\n", + "# 用百分号隔开,括号括起来\n", + "t = \"%s %f, %d\" % (s, x, y)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'some numbers: 1.340000, 2'" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/02. python essentials/02.06 lists.ipynb b/02. python essentials/02.06 lists.ipynb index 6456a528..84336636 100644 --- a/02. python essentials/02.06 lists.ipynb +++ b/02. python essentials/02.06 lists.ipynb @@ -1,1248 +1,1248 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 列表" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "在**Python**中,列表是一个有序的序列。\n", - "\n", - "列表用一对 `[]` 生成,中间的元素用 `,` 隔开,其中的元素不需要是同一类型,同时列表的长度也不固定。" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2.0, 'hello']\n" - ] - } - ], - "source": [ - "l = [1, 2.0, 'hello']\n", - "print l" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "空列表可以用 `[]` 或者 `list()` 生成:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "empty_list = []\n", - "empty_list" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "empty_list = list()\n", - "empty_list" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 列表操作" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "与字符串类似,列表也支持以下的操作:" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 长度" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "用 `len` 查看列表长度:" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(l)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 加法和乘法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "列表加法,相当于将两个列表按顺序连接:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 3.2, 'hello']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = [1, 2, 3]\n", - "b = [3.2, 'hello']\n", - "a + b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "列表与整数相乘,相当于将列表重复相加:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2.0, 'hello', 1, 2.0, 'hello']" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "l * 2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 索引和分片" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "列表和字符串一样可以通过索引和分片来查看它的元素。" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "索引:" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = [10, 11, 12, 13, 14]\n", - "a[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "反向索引:" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "14" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "分片:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[12, 13]" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a[2:-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "与字符串不同的是,列表可以通过索引和分片来修改。\n", - "\n", - "对于字符串,如果我们通过索引或者分片来修改,**Python**会报错:" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'str' object does not support item assignment", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"hello world\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;31m# 把开头的 h 改成大写\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'H'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" - ] - } - ], - "source": [ - "s = \"hello world\"\n", - "# 把开头的 h 改成大写\n", - "s[0] = 'H'" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "而这种操作对于列表来说是可以的:" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[100, 11, 12, 13, 14]\n" - ] - } - ], - "source": [ - "a = [10, 11, 12, 13, 14]\n", - "a[0] = 100\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "这种赋值也适用于分片,例如,将列表的第2,3两个元素换掉:" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[100, 1, 2, 13, 14]" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a[1:3] = [1, 2]\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "事实上,对于连续的分片(即步长为 `1` ),**Python**采用的是整段替换的方法,两者的元素个数并不需要相同,例如,将 `[11,12]` 替换为 `[1,2,3,4]`:" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 1, 2, 3, 4, 13, 14]\n" - ] - } - ], - "source": [ - "a = [10, 11, 12, 13, 14]\n", - "a[1:3] = [1, 2, 3, 4]\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "这意味着,可以用这种方法来删除列表中一个连续的分片:" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2]\n", - "[10, 11, 12]\n" - ] - } - ], - "source": [ - "a = [10, 1, 2, 11, 12]\n", - "print a[1:3]\n", - "a[1:3] = []\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "对于不连续(间隔step不为1)的片段进行修改时,两者的元素数目必须一致:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 11, 2, 13, 3]" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = [10, 11, 12, 13, 14]\n", - "a[::2] = [1, 2, 3]\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "否则会报错:" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "ename": "ValueError", - "evalue": "attempt to assign sequence of size 0 to extended slice of size 3", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mValueError\u001b[0m: attempt to assign sequence of size 0 to extended slice of size 3" - ] - } - ], - "source": [ - "a[::2] = []" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 删除元素" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python**提供了删除列表中元素的方法 'del'。\n", - "\n", - "删除列表中的第一个元素:" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['a', 'b', 'c']\n" - ] - } - ], - "source": [ - "a = [1002, 'a', 'b', 'c']\n", - "del a[0]\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "删除第2到最后一个元素:" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1002]" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = [1002, 'a', 'b', 'c']\n", - "del a[1:]\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "删除间隔的元素:" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2]" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = ['a', 1, 'b', 2, 'c']\n", - "del a[::2]\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###测试从属关系" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "用 `in` 来看某个元素是否在某个序列(不仅仅是列表)中,用not in来判断是否不在某个序列中。" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n" - ] - } - ], - "source": [ - "a = [10, 11, 12, 13, 14]\n", - "print 10 in a\n", - "print 10 not in a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "也可以作用于字符串:" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n" - ] - } - ], - "source": [ - "s = 'hello world'\n", - "print 'he' in s\n", - "print 'world' not in s" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "列表中可以包含各种对象,甚至可以包含列表:" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[12, 13]" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = [10, 'eleven', [12, 13]]\n", - "a[2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "a[2]是列表,可以对它再进行索引:" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "13" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a[2][1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 列表方法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 不改变列表的方法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 列表中某个元素个数count" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.count(ob)` 返回列表中元素 `ob` 出现的次数。" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = [11, 12, 13, 12, 11]\n", - "a.count(11)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 列表中某个元素位置index" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.index(ob)` 返回列表中元素 `ob` 第一次出现的索引位置,如果 `ob` 不在 `l` 中会报错。" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.index(12)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "不存在的元素会报错:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "ValueError", - "evalue": "1 is not in list", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mValueError\u001b[0m: 1 is not in list" - ] - } - ], - "source": [ - "a.index(1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 改变列表的方法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 向列表添加单个元素" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.append(ob)` 将元素 `ob` 添加到列表 `l` 的最后。" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 11, 12, 11]\n" - ] - } - ], - "source": [ - "a = [10, 11, 12]\n", - "a.append(11)\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "append每次只添加一个元素,并不会因为这个元素是序列而将其展开:" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 11, 12, 11, [11, 12]]\n" - ] - } - ], - "source": [ - "a.append([11, 12])\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 向列表添加序列" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.extend(lst)` 将序列 `lst` 的元素依次添加到列表 `l` 的最后,作用相当于 `l += lst`。" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 11, 12, 11, 1, 2]\n" - ] - } - ], - "source": [ - "a = [10, 11, 12, 11]\n", - "a.extend([1, 2])\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 插入元素" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.insert(idx, ob)` 在索引 `idx` 处插入 `ob` ,之后的元素依次后移。" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 11, 12, 'a', 13, 11]\n" - ] - } - ], - "source": [ - "a = [10, 11, 12, 13, 11]\n", - "# 在索引 3 插入 'a'\n", - "a.insert(3, 'a')\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 移除元素" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.remove(ob)` 会将列表中第一个出现的 `ob` 删除,如果 `ob` 不在 `l` 中会报错。" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 12, 13, 11]\n" - ] - } - ], - "source": [ - "a = [10, 11, 12, 13, 11]\n", - "# 移除了第一个 11\n", - "a.remove(11)\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 弹出元素" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.pop(idx)` 会将索引 `idx` 处的元素删除,并返回这个元素。" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "12" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = [10, 11, 12, 13, 11]\n", - "a.pop(2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 排序" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.sort()` 会将列表中的元素按照一定的规则排序:" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 10, 11, 11, 13]\n" - ] - } - ], - "source": [ - "a = [10, 1, 11, 13, 11, 2]\n", - "a.sort()\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "如果不想改变原来列表中的值,可以使用 `sorted` 函数:" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 1, 11, 13, 11, 2]\n", - "[1, 2, 10, 11, 11, 13]\n" - ] - } - ], - "source": [ - "a = [10, 1, 11, 13, 11, 2]\n", - "b = sorted(a)\n", - "print a\n", - "print b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 列表反向" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`l.reverse()` 会将列表中的元素从后向前排列。" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[6, 5, 4, 3, 2, 1]\n" - ] - } - ], - "source": [ - "a = [1, 2, 3, 4, 5, 6]\n", - "a.reverse()\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "如果不想改变原来列表中的值,可以使用这样的方法:" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5, 6]\n", - "[6, 5, 4, 3, 2, 1]\n" - ] - } - ], - "source": [ - "a = [1, 2, 3, 4, 5, 6]\n", - "b = a[::-1]\n", - "print a\n", - "print b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "如果不清楚用法,可以查看帮助:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "a.sort?" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 列表" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在**Python**中,列表是一个有序的序列。\n", + "\n", + "列表用一对 `[]` 生成,中间的元素用 `,` 隔开,其中的元素不需要是同一类型,同时列表的长度也不固定。" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2.0, 'hello']\n" + ] + } + ], + "source": [ + "l = [1, 2.0, 'hello']\n", + "print l" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "空列表可以用 `[]` 或者 `list()` 生成:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "empty_list = []\n", + "empty_list" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "empty_list = list()\n", + "empty_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 列表操作" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "与字符串类似,列表也支持以下的操作:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 长度" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "用 `len` 查看列表长度:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(l)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 加法和乘法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "列表加法,相当于将两个列表按顺序连接:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 3.2, 'hello']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [1, 2, 3]\n", + "b = [3.2, 'hello']\n", + "a + b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "列表与整数相乘,相当于将列表重复相加:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2.0, 'hello', 1, 2.0, 'hello']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l * 2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 索引和分片" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "列表和字符串一样可以通过索引和分片来查看它的元素。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "索引:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [10, 11, 12, 13, 14]\n", + "a[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "反向索引:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "分片:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[12, 13]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[2:-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "与字符串不同的是,列表可以通过索引和分片来修改。\n", + "\n", + "对于字符串,如果我们通过索引或者分片来修改,**Python**会报错:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"hello world\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;31m# 把开头的 h 改成大写\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'H'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "s = \"hello world\"\n", + "# 把开头的 h 改成大写\n", + "s[0] = 'H'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "而这种操作对于列表来说是可以的:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[100, 11, 12, 13, 14]\n" + ] + } + ], + "source": [ + "a = [10, 11, 12, 13, 14]\n", + "a[0] = 100\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "这种赋值也适用于分片,例如,将列表的第2,3两个元素换掉:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[100, 1, 2, 13, 14]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[1:3] = [1, 2]\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "事实上,对于连续的分片(即步长为 `1` ),**Python**采用的是整段替换的方法,两者的元素个数并不需要相同,例如,将 `[11,12]` 替换为 `[1,2,3,4]`:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 1, 2, 3, 4, 13, 14]\n" + ] + } + ], + "source": [ + "a = [10, 11, 12, 13, 14]\n", + "a[1:3] = [1, 2, 3, 4]\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "这意味着,可以用这种方法来删除列表中一个连续的分片:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2]\n", + "[10, 11, 12]\n" + ] + } + ], + "source": [ + "a = [10, 1, 2, 11, 12]\n", + "print a[1:3]\n", + "a[1:3] = []\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "对于不连续(间隔step不为1)的片段进行修改时,两者的元素数目必须一致:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 11, 2, 13, 3]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [10, 11, 12, 13, 14]\n", + "a[::2] = [1, 2, 3]\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "否则会报错:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "ename": "ValueError", + "evalue": "attempt to assign sequence of size 0 to extended slice of size 3", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mValueError\u001b[0m: attempt to assign sequence of size 0 to extended slice of size 3" + ] + } + ], + "source": [ + "a[::2] = []" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 删除元素" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python**提供了删除列表中元素的方法 'del'。\n", + "\n", + "删除列表中的第一个元素:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b', 'c']\n" + ] + } + ], + "source": [ + "a = [1002, 'a', 'b', 'c']\n", + "del a[0]\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "删除第2到最后一个元素:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1002]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [1002, 'a', 'b', 'c']\n", + "del a[1:]\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "删除间隔的元素:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = ['a', 1, 'b', 2, 'c']\n", + "del a[::2]\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 测试从属关系" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "用 `in` 来看某个元素是否在某个序列(不仅仅是列表)中,用not in来判断是否不在某个序列中。" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "a = [10, 11, 12, 13, 14]\n", + "print 10 in a\n", + "print 10 not in a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "也可以作用于字符串:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "s = 'hello world'\n", + "print 'he' in s\n", + "print 'world' not in s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "列表中可以包含各种对象,甚至可以包含列表:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[12, 13]" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [10, 'eleven', [12, 13]]\n", + "a[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "a[2]是列表,可以对它再进行索引:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[2][1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 列表方法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 不改变列表的方法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 列表中某个元素个数count" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.count(ob)` 返回列表中元素 `ob` 出现的次数。" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [11, 12, 13, 12, 11]\n", + "a.count(11)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 列表中某个元素位置index" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.index(ob)` 返回列表中元素 `ob` 第一次出现的索引位置,如果 `ob` 不在 `l` 中会报错。" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.index(12)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "不存在的元素会报错:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "ValueError", + "evalue": "1 is not in list", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mValueError\u001b[0m: 1 is not in list" + ] + } + ], + "source": [ + "a.index(1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 改变列表的方法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 向列表添加单个元素" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.append(ob)` 将元素 `ob` 添加到列表 `l` 的最后。" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 11, 12, 11]\n" + ] + } + ], + "source": [ + "a = [10, 11, 12]\n", + "a.append(11)\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "append每次只添加一个元素,并不会因为这个元素是序列而将其展开:" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 11, 12, 11, [11, 12]]\n" + ] + } + ], + "source": [ + "a.append([11, 12])\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 向列表添加序列" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.extend(lst)` 将序列 `lst` 的元素依次添加到列表 `l` 的最后,作用相当于 `l += lst`。" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 11, 12, 11, 1, 2]\n" + ] + } + ], + "source": [ + "a = [10, 11, 12, 11]\n", + "a.extend([1, 2])\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 插入元素" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.insert(idx, ob)` 在索引 `idx` 处插入 `ob` ,之后的元素依次后移。" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 11, 12, 'a', 13, 11]\n" + ] + } + ], + "source": [ + "a = [10, 11, 12, 13, 11]\n", + "# 在索引 3 插入 'a'\n", + "a.insert(3, 'a')\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 移除元素" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.remove(ob)` 会将列表中第一个出现的 `ob` 删除,如果 `ob` 不在 `l` 中会报错。" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 12, 13, 11]\n" + ] + } + ], + "source": [ + "a = [10, 11, 12, 13, 11]\n", + "# 移除了第一个 11\n", + "a.remove(11)\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 弹出元素" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.pop(idx)` 会将索引 `idx` 处的元素删除,并返回这个元素。" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [10, 11, 12, 13, 11]\n", + "a.pop(2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 排序" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.sort()` 会将列表中的元素按照一定的规则排序:" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 10, 11, 11, 13]\n" + ] + } + ], + "source": [ + "a = [10, 1, 11, 13, 11, 2]\n", + "a.sort()\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "如果不想改变原来列表中的值,可以使用 `sorted` 函数:" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 1, 11, 13, 11, 2]\n", + "[1, 2, 10, 11, 11, 13]\n" + ] + } + ], + "source": [ + "a = [10, 1, 11, 13, 11, 2]\n", + "b = sorted(a)\n", + "print a\n", + "print b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 列表反向" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`l.reverse()` 会将列表中的元素从后向前排列。" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6, 5, 4, 3, 2, 1]\n" + ] + } + ], + "source": [ + "a = [1, 2, 3, 4, 5, 6]\n", + "a.reverse()\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "如果不想改变原来列表中的值,可以使用这样的方法:" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6]\n", + "[6, 5, 4, 3, 2, 1]\n" + ] + } + ], + "source": [ + "a = [1, 2, 3, 4, 5, 6]\n", + "b = a[::-1]\n", + "print a\n", + "print b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "如果不清楚用法,可以查看帮助:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a.sort?" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/02. python essentials/02.09 speed comparison between list & tuple.ipynb b/02. python essentials/02.09 speed comparison between list & tuple.ipynb index db179bb5..ae80598d 100644 --- a/02. python essentials/02.09 speed comparison between list & tuple.ipynb +++ b/02. python essentials/02.09 speed comparison between list & tuple.ipynb @@ -1,217 +1,217 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 列表与元组的速度比较" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "IPython中用 `magic` 命令 `%timeit` 来计时。" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##比较生成速度" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1000000 loops, best of 3: 456 ns per loop\n" - ] - } - ], - "source": [ - "%timeit [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "10000000 loops, best of 3: 23 ns per loop\n" - ] - } - ], - "source": [ - "%timeit (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以看到,元组的生成速度要比列表的生成速度快得多,相差大概一个数量级。" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 比较遍历速度" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "产生内容相同的随机列表和元组:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "from numpy.random import rand\n", - "values = rand(10000,4)\n", - "lst = [list(row) for row in values]\n", - "tup = tuple(tuple(row) for row in values)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "100 loops, best of 3: 4.12 ms per loop\n" - ] - } - ], - "source": [ - " %timeit for row in lst: list(row)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "100 loops, best of 3: 2.07 ms per loop\n" - ] - } - ], - "source": [ - "%timeit for row in tup: tuple(row)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "在遍历上,元组和列表的速度表现差不多。" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 比较遍历和索引速度:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The slowest run took 12.20 times longer than the fastest. This could mean that an intermediate result is being cached \n", - "100 loops, best of 3: 3.73 ms per loop\n" - ] - } - ], - "source": [ - "%timeit for row in lst: a = row[0] + 1" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "100 loops, best of 3: 3.82 ms per loop\n" - ] - } - ], - "source": [ - "%timeit for row in tup: a = row[0] + 1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "元组的生成速度会比列表快很多,迭代速度快一点,索引速度差不多。" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.9" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 列表与元组的速度比较" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "IPython 中用 `magic` 命令 `%timeit` 来计时。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 比较生成速度" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1000000 loops, best of 3: 456 ns per loop\n" + ] + } + ], + "source": [ + "%timeit [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10000000 loops, best of 3: 23 ns per loop\n" + ] + } + ], + "source": [ + "%timeit (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以看到,元组的生成速度要比列表的生成速度快得多,相差大概一个数量级。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 比较遍历速度" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "产生内容相同的随机列表和元组:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from numpy.random import rand\n", + "values = rand(10000,4)\n", + "lst = [list(row) for row in values]\n", + "tup = tuple(tuple(row) for row in values)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100 loops, best of 3: 4.12 ms per loop\n" + ] + } + ], + "source": [ + " %timeit for row in lst: list(row)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100 loops, best of 3: 2.07 ms per loop\n" + ] + } + ], + "source": [ + "%timeit for row in tup: tuple(row)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在遍历上,元组和列表的速度表现差不多。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 比较遍历和索引速度:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The slowest run took 12.20 times longer than the fastest. This could mean that an intermediate result is being cached \n", + "100 loops, best of 3: 3.73 ms per loop\n" + ] + } + ], + "source": [ + "%timeit for row in lst: a = row[0] + 1" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100 loops, best of 3: 3.82 ms per loop\n" + ] + } + ], + "source": [ + "%timeit for row in tup: a = row[0] + 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "元组的生成速度会比列表快很多,迭代速度快一点,索引速度差不多。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/02. python essentials/02.10 dictionaries.ipynb b/02. python essentials/02.10 dictionaries.ipynb index d5821afe..cf112c17 100644 --- a/02. python essentials/02.10 dictionaries.ipynb +++ b/02. python essentials/02.10 dictionaries.ipynb @@ -1,1165 +1,1165 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#字典 " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "字典 `dictionary` ,在一些编程语言中也称为 `hash` , `map` ,是一种由键值对组成的数据结构。\n", - "\n", - "顾名思义,我们把键想象成字典中的单词,值想象成词对应的定义,那么——\n", - "\n", - "一个词可以对应一个或者多个定义,但是这些定义只能通过这个词来进行查询。" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 基本操作" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 空字典" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Python** 使用 `{}` 或者 `dict()` 来创建一个空的字典:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "dict" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = {}\n", - "type(a)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "dict" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = dict()\n", - "type(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "有了dict之后,可以用索引键值的方法向其中添加元素,也可以通过索引来查看元素的值:" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 插入键值" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'one': 'this is number 1', 'two': 'this is number 2'}" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a[\"one\"] = \"this is number 1\"\n", - "a[\"two\"] = \"this is number 2\"\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 查看键值" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'this is number 1'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a['one']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 更新键值" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'one': 'this is number 1, too', 'two': 'this is number 2'}" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a[\"one\"] = \"this is number 1, too\"\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 初始化字典" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可以看到,Python使用`key: value`这样的结构来表示字典中的元素结构,事实上,可以直接使用这样的结构来初始化一个字典:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'this is number 1'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "b = {'one': 'this is number 1', 'two': 'this is number 2'}\n", - "b['one']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 字典没有顺序" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "当我们 `print` 一个字典时,**Python**并不一定按照插入键值的先后顺序进行显示,因为字典中的键本身不一定是有序的。" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'two': 'this is number 2', 'one': 'this is number 1, too'}\n" - ] - } - ], - "source": [ - "print a" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'two': 'this is number 2', 'one': 'this is number 1'}\n" - ] - } - ], - "source": [ - "print b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "因此,**Python**中不能用支持用数字索引按顺序查看字典中的值,而且数字本身也有可能成为键值,这样会引起混淆:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "KeyError", - "evalue": "0", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# 会报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m: 0" - ] - } - ], - "source": [ - "# 会报错\n", - "a[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 键必须是不可变的类型" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "出于hash的目的,Python中要求这些键值对的**键**必须是**不可变**的,而值可以是任意的Python对象。\n", - "\n", - "一个表示近义词的字典:" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'immutable': ['fixed',\n", - " 'set',\n", - " 'rigid',\n", - " 'inflexible',\n", - " 'permanent',\n", - " 'established',\n", - " 'carved in stone'],\n", - " 'mutable': ['changeable',\n", - " 'variable',\n", - " 'varying',\n", - " 'fluctuating',\n", - " 'shifting',\n", - " 'inconsistent',\n", - " 'unpredictable',\n", - " 'inconstant',\n", - " 'fickle',\n", - " 'uneven',\n", - " 'unstable',\n", - " 'protean']}" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "synonyms = {}\n", - "synonyms['mutable'] = ['changeable', 'variable', 'varying', 'fluctuating',\n", - " 'shifting', 'inconsistent', 'unpredictable', 'inconstant',\n", - " 'fickle', 'uneven', 'unstable', 'protean']\n", - "synonyms['immutable'] = ['fixed', 'set', 'rigid', 'inflexible', \n", - " 'permanent', 'established', 'carved in stone']\n", - "synonyms" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "另一个例子:" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{500: {'mag': 0.05, 'width': 20},\n", - " 760: {'mag': 0.04, 'width': 25},\n", - " 3001: {'mag': 0.05, 'width': 80},\n", - " 4180: {'mag': 0.03, 'width': 30}}" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# 定义四个字典\n", - "e1 = {'mag': 0.05, 'width': 20}\n", - "e2 = {'mag': 0.04, 'width': 25}\n", - "e3 = {'mag': 0.05, 'width': 80}\n", - "e4 = {'mag': 0.03, 'width': 30}\n", - "# 以字典作为值传入新的字典\n", - "events = {500: e1, 760: e2, 3001: e3, 4180: e4}\n", - "events" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "键(或者值)的数据类型可以不同:" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[{'first': 'Sam', 'last': 'Malone', 'name': 35},\n", - " {'first': 'Woody', 'last': 'Boyd', 'name': 21},\n", - " {'first': 'Norm', 'last': 'Peterson', 'name': 34},\n", - " {'first': 'Diane', 'last': 'Chambers', 'name': 33}]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "people = [\n", - " {'first': 'Sam', 'last': 'Malone', 'name': 35},\n", - " {'first': 'Woody', 'last': 'Boyd', 'name': 21},\n", - " {'first': 'Norm', 'last': 'Peterson', 'name': 34},\n", - " {'first': 'Diane', 'last': 'Chambers', 'name': 33}\n", - "]\n", - "people" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 使用 dict 初始化字典" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "除了通常的定义方式,还可以通过 `dict()` 转化来生成字典:" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'foozelator': 123, 'frombicator': 18, 'snitzelhogen': 23, 'spatzleblock': 34}" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "inventory = dict(\n", - " [('foozelator', 123),\n", - " ('frombicator', 18), \n", - " ('spatzleblock', 34), \n", - " ('snitzelhogen', 23)\n", - " ])\n", - "inventory" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "利用索引直接更新键值对:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'foozelator': 123, 'frombicator': 19, 'snitzelhogen': 23, 'spatzleblock': 34}" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "inventory['frombicator'] += 1\n", - "inventory" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 适合做键的类型" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "在不可变类型中,整数和字符串是字典中最常用的类型;而浮点数通常不推荐用来做键,原因如下:" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "KeyError", - "evalue": "3.3", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1.1\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m2.2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m6.6\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;31m# 会报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3.3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m: 3.3" - ] - } - ], - "source": [ - "data = {}\n", - "data[1.1 + 2.2] = 6.6\n", - "# 会报错\n", - "data[3.3]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "事实上,观察`data`的值就会发现,这个错误是由浮点数的精度问题所引起的:" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{3.3000000000000003: 6.6}" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "有时候,也可以使用元组作为键值,例如,可以用元组做键来表示从第一个城市飞往第二个城市航班数的多少:" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "connections = {}\n", - "connections[('New York', 'Seattle')] = 100\n", - "connections[('Austin', 'New York')] = 200\n", - "connections[('New York', 'Austin')] = 400" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "元组是有序的,因此 `('New York', 'Austin')` 和 `('Austin', 'New York')` 是两个不同的键:" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "200\n", - "400\n" - ] - } - ], - "source": [ - "print connections[('Austin', 'New York')]\n", - "print connections[('New York', 'Austin')]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 字典方法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### `get` 方法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "之前已经见过,用索引可以找到一个键对应的值,但是当字典中没有这个键的时候,Python会报错,这时候可以使用字典的 `get` 方法来处理这种情况,其用法如下:\n", - "\n", - " `d.get(key, default = None)`\n", - "\n", - "返回字典中键 `key` 对应的值,如果没有这个键,返回 `default` 指定的值(默认是 `None` )。" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "a = {}\n", - "a[\"one\"] = \"this is number 1\"\n", - "a[\"two\"] = \"this is number 2\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "索引不存在的键值会报错:" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "KeyError", - "evalue": "'three'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"three\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m: 'three'" - ] - } - ], - "source": [ - "a[\"three\"]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "改用get方法:" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "None\n" - ] - } - ], - "source": [ - "print a.get(\"three\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "指定默认值参数:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'undefined'" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.get(\"three\", \"undefined\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### `pop` 方法删除元素" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`pop` 方法可以用来弹出字典中某个键对应的值,同时也可以指定默认参数:\n", - "\n", - " `d.pop(key, default = None)`\n", - "\n", - "删除并返回字典中键 `key` 对应的值,如果没有这个键,返回 `default` 指定的值(默认是 `None` )。" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'one': 'this is number 1', 'two': 'this is number 2'}" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "弹出并返回值:" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'this is number 2'" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.pop(\"two\")" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'one': 'this is number 1'}" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "弹出不存在的键值:" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'not exist'" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a.pop(\"two\", 'not exist')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "与列表一样,`del` 函数可以用来删除字典中特定的键值对,例如:" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{}" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "del a[\"one\"]\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### `update`方法更新字典" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "之前已经知道,可以通过索引来插入、修改单个键值对,但是如果想对多个键值对进行操作,这种方法就显得比较麻烦,好在有 `update` 方法:\n", - "\n", - " `d.update(newd)`\n", - "\n", - "将字典`newd`中的内容更新到`d`中去。" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'born': 1831, 'last': 'Maxwell', 'first': 'Jmes'}\n" - ] - } - ], - "source": [ - "person = {}\n", - "person['first'] = \"Jmes\"\n", - "person['last'] = \"Maxwell\"\n", - "person['born'] = 1831\n", - "print person" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "把'first'改成'James',同时插入'middle'的值'Clerk':" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'middle': 'Clerk', 'born': 1831, 'last': 'Maxwell', 'first': 'James'}\n" - ] - } - ], - "source": [ - "person_modifications = {'first': 'James', 'middle': 'Clerk'}\n", - "person.update(person_modifications)\n", - "print person" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### `in`查询字典中是否有该键" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "barn = {'cows': 1, 'dogs': 5, 'cats': 3}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`in` 可以用来判断字典中是否有某个特定的键:" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'chickens' in barn" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'cows' in barn" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### `keys` 方法,`values` 方法和`items` 方法" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " `d.keys()` \n", - "\n", - "返回一个由所有键组成的列表;\n", - "\n", - " `d.values()` \n", - "\n", - "返回一个由所有值组成的列表;\n", - "\n", - " `d.items()` \n", - "\n", - "返回一个由所有键值对元组组成的列表;" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['cows', 'cats', 'dogs']" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "barn.keys()" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 3, 5]" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "barn.values()" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[('cows', 1), ('cats', 3), ('dogs', 5)]" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "barn.items()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "注意一件事:`keys()` 返回的值跟 `values()` 返回的值是一一对应的。" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "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.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 字典 " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "字典 `dictionary` ,在一些编程语言中也称为 `hash` , `map` ,是一种由键值对组成的数据结构。\n", + "\n", + "顾名思义,我们把键想象成字典中的单词,值想象成词对应的定义,那么——\n", + "\n", + "一个词可以对应一个或者多个定义,但是这些定义只能通过这个词来进行查询。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 基本操作" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 空字典" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Python** 使用 `{}` 或者 `dict()` 来创建一个空的字典:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = {}\n", + "type(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = dict()\n", + "type(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "有了dict之后,可以用索引键值的方法向其中添加元素,也可以通过索引来查看元素的值:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 插入键值" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'one': 'this is number 1', 'two': 'this is number 2'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[\"one\"] = \"this is number 1\"\n", + "a[\"two\"] = \"this is number 2\"\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 查看键值" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'this is number 1'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a['one']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 更新键值" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'one': 'this is number 1, too', 'two': 'this is number 2'}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[\"one\"] = \"this is number 1, too\"\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 初始化字典" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以看到,Python使用`key: value`这样的结构来表示字典中的元素结构,事实上,可以直接使用这样的结构来初始化一个字典:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'this is number 1'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = {'one': 'this is number 1', 'two': 'this is number 2'}\n", + "b['one']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 字典没有顺序" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "当我们 `print` 一个字典时,**Python**并不一定按照插入键值的先后顺序进行显示,因为字典中的键本身不一定是有序的。" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'two': 'this is number 2', 'one': 'this is number 1, too'}\n" + ] + } + ], + "source": [ + "print a" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'two': 'this is number 2', 'one': 'this is number 1'}\n" + ] + } + ], + "source": [ + "print b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "因此,**Python**中不能用支持用数字索引按顺序查看字典中的值,而且数字本身也有可能成为键值,这样会引起混淆:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "KeyError", + "evalue": "0", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# 会报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m: 0" + ] + } + ], + "source": [ + "# 会报错\n", + "a[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 键必须是不可变的类型" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "出于hash的目的,Python中要求这些键值对的**键**必须是**不可变**的,而值可以是任意的Python对象。\n", + "\n", + "一个表示近义词的字典:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'immutable': ['fixed',\n", + " 'set',\n", + " 'rigid',\n", + " 'inflexible',\n", + " 'permanent',\n", + " 'established',\n", + " 'carved in stone'],\n", + " 'mutable': ['changeable',\n", + " 'variable',\n", + " 'varying',\n", + " 'fluctuating',\n", + " 'shifting',\n", + " 'inconsistent',\n", + " 'unpredictable',\n", + " 'inconstant',\n", + " 'fickle',\n", + " 'uneven',\n", + " 'unstable',\n", + " 'protean']}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "synonyms = {}\n", + "synonyms['mutable'] = ['changeable', 'variable', 'varying', 'fluctuating',\n", + " 'shifting', 'inconsistent', 'unpredictable', 'inconstant',\n", + " 'fickle', 'uneven', 'unstable', 'protean']\n", + "synonyms['immutable'] = ['fixed', 'set', 'rigid', 'inflexible', \n", + " 'permanent', 'established', 'carved in stone']\n", + "synonyms" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "另一个例子:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{500: {'mag': 0.05, 'width': 20},\n", + " 760: {'mag': 0.04, 'width': 25},\n", + " 3001: {'mag': 0.05, 'width': 80},\n", + " 4180: {'mag': 0.03, 'width': 30}}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 定义四个字典\n", + "e1 = {'mag': 0.05, 'width': 20}\n", + "e2 = {'mag': 0.04, 'width': 25}\n", + "e3 = {'mag': 0.05, 'width': 80}\n", + "e4 = {'mag': 0.03, 'width': 30}\n", + "# 以字典作为值传入新的字典\n", + "events = {500: e1, 760: e2, 3001: e3, 4180: e4}\n", + "events" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "键(或者值)的数据类型可以不同:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'first': 'Sam', 'last': 'Malone', 'name': 35},\n", + " {'first': 'Woody', 'last': 'Boyd', 'name': 21},\n", + " {'first': 'Norm', 'last': 'Peterson', 'name': 34},\n", + " {'first': 'Diane', 'last': 'Chambers', 'name': 33}]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "people = [\n", + " {'first': 'Sam', 'last': 'Malone', 'name': 35},\n", + " {'first': 'Woody', 'last': 'Boyd', 'name': 21},\n", + " {'first': 'Norm', 'last': 'Peterson', 'name': 34},\n", + " {'first': 'Diane', 'last': 'Chambers', 'name': 33}\n", + "]\n", + "people" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 使用 dict 初始化字典" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "除了通常的定义方式,还可以通过 `dict()` 转化来生成字典:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'foozelator': 123, 'frombicator': 18, 'snitzelhogen': 23, 'spatzleblock': 34}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory = dict(\n", + " [('foozelator', 123),\n", + " ('frombicator', 18), \n", + " ('spatzleblock', 34), \n", + " ('snitzelhogen', 23)\n", + " ])\n", + "inventory" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "利用索引直接更新键值对:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'foozelator': 123, 'frombicator': 19, 'snitzelhogen': 23, 'spatzleblock': 34}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory['frombicator'] += 1\n", + "inventory" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 适合做键的类型" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在不可变类型中,整数和字符串是字典中最常用的类型;而浮点数通常不推荐用来做键,原因如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "KeyError", + "evalue": "3.3", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1.1\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m2.2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m6.6\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;31m# 会报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3.3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m: 3.3" + ] + } + ], + "source": [ + "data = {}\n", + "data[1.1 + 2.2] = 6.6\n", + "# 会报错\n", + "data[3.3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "事实上,观察`data`的值就会发现,这个错误是由浮点数的精度问题所引起的:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{3.3000000000000003: 6.6}" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "有时候,也可以使用元组作为键值,例如,可以用元组做键来表示从第一个城市飞往第二个城市航班数的多少:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "connections = {}\n", + "connections[('New York', 'Seattle')] = 100\n", + "connections[('Austin', 'New York')] = 200\n", + "connections[('New York', 'Austin')] = 400" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "元组是有序的,因此 `('New York', 'Austin')` 和 `('Austin', 'New York')` 是两个不同的键:" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "400\n" + ] + } + ], + "source": [ + "print connections[('Austin', 'New York')]\n", + "print connections[('New York', 'Austin')]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 字典方法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `get` 方法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "之前已经见过,用索引可以找到一个键对应的值,但是当字典中没有这个键的时候,Python会报错,这时候可以使用字典的 `get` 方法来处理这种情况,其用法如下:\n", + "\n", + " `d.get(key, default = None)`\n", + "\n", + "返回字典中键 `key` 对应的值,如果没有这个键,返回 `default` 指定的值(默认是 `None` )。" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = {}\n", + "a[\"one\"] = \"this is number 1\"\n", + "a[\"two\"] = \"this is number 2\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "索引不存在的键值会报错:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'three'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"three\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m: 'three'" + ] + } + ], + "source": [ + "a[\"three\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "改用get方法:" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], + "source": [ + "print a.get(\"three\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "指定默认值参数:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'undefined'" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.get(\"three\", \"undefined\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `pop` 方法删除元素" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`pop` 方法可以用来弹出字典中某个键对应的值,同时也可以指定默认参数:\n", + "\n", + " `d.pop(key, default = None)`\n", + "\n", + "删除并返回字典中键 `key` 对应的值,如果没有这个键,返回 `default` 指定的值(默认是 `None` )。" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'one': 'this is number 1', 'two': 'this is number 2'}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "弹出并返回值:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'this is number 2'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop(\"two\")" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'one': 'this is number 1'}" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "弹出不存在的键值:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'not exist'" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.pop(\"two\", 'not exist')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "与列表一样,`del` 函数可以用来删除字典中特定的键值对,例如:" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "del a[\"one\"]\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `update`方法更新字典" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "之前已经知道,可以通过索引来插入、修改单个键值对,但是如果想对多个键值对进行操作,这种方法就显得比较麻烦,好在有 `update` 方法:\n", + "\n", + " `d.update(newd)`\n", + "\n", + "将字典`newd`中的内容更新到`d`中去。" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'born': 1831, 'last': 'Maxwell', 'first': 'Jmes'}\n" + ] + } + ], + "source": [ + "person = {}\n", + "person['first'] = \"Jmes\"\n", + "person['last'] = \"Maxwell\"\n", + "person['born'] = 1831\n", + "print person" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "把'first'改成'James',同时插入'middle'的值'Clerk':" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'middle': 'Clerk', 'born': 1831, 'last': 'Maxwell', 'first': 'James'}\n" + ] + } + ], + "source": [ + "person_modifications = {'first': 'James', 'middle': 'Clerk'}\n", + "person.update(person_modifications)\n", + "print person" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `in`查询字典中是否有该键" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "barn = {'cows': 1, 'dogs': 5, 'cats': 3}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`in` 可以用来判断字典中是否有某个特定的键:" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'chickens' in barn" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'cows' in barn" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `keys` 方法,`values` 方法和`items` 方法" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " `d.keys()` \n", + "\n", + "返回一个由所有键组成的列表;\n", + "\n", + " `d.values()` \n", + "\n", + "返回一个由所有值组成的列表;\n", + "\n", + " `d.items()` \n", + "\n", + "返回一个由所有键值对元组组成的列表;" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['cows', 'cats', 'dogs']" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "barn.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 5]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "barn.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('cows', 1), ('cats', 3), ('dogs', 5)]" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "barn.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "注意一件事:`keys()` 返回的值跟 `values()` 返回的值是一一对应的。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/09. theano/09.12 random streams.ipynb b/09. theano/09.12 random streams.ipynb index 67355e76..288a18b3 100644 --- a/09. theano/09.12 random streams.ipynb +++ b/09. theano/09.12 random streams.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Theano 变量:随机数流" + "# Theano 随机数流变量" ] }, { @@ -32,7 +32,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "`Theano` 的随机数流由 `theano.sandbox.rng_mrg` 中的 `MRG_RandomStreams` 实现(`sandbox` 表示是实验代码):" + "`Theano` 的随机数变量由 `theano.sandbox.rng_mrg` 中的 `MRG_RandomStreams` 实现(`sandbox` 表示是实验代码):" ] }, { @@ -79,7 +79,7 @@ "- `multinomial(size=None, n=1, pvals=None, ndim=None, dtype='int64', nstreams=None)`\n", " - 产生指定形状的、服从多项分布的随机数变量\n", "\n", - "与 np.random.random 不同,它产生的是随机数变量,而不是随机数数组:" + "与 np.random.random 不同,它产生的是随机数变量,而不是随机数数组,因此可以将 `size` 作为参数传给它:" ] }, { diff --git a/09. theano/09.15 tensor module.ipynb b/09. theano/09.15 tensor module.ipynb new file mode 100644 index 00000000..fbf1ea4b --- /dev/null +++ b/09. theano/09.15 tensor module.ipynb @@ -0,0 +1,156 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Theano 张量 tensor 模块 " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "张量是向量在数学上的一种推广,具体内容可以参考维基百科:\n", + "https://en.wikipedia.org/wiki/Tensor\n", + "\n", + "在 Theano 中有一个专门处理张量变量的模块:`theano.tensor` (以下简称 `T`)。" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Using gpu device 1: Tesla C2075 (CNMeM is disabled)\n" + ] + } + ], + "source": [ + "import theano\n", + "import theano.tensor as T" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 构造符号变量" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "可以用 `tensor` 模块创造符号变量:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], + "source": [ + "x = T.fmatrix()\n", + "\n", + "print type(x)\n", + "print type(T.fmatrix)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "从上面可以看到,`T.fmatrix()` 创造出的是一个 `TensorVariable` 类,而 `T.fmatrix` 本身是一个 `TensorType` 类。\n", + "\n", + "除了使用 `fmatrix`,我们还可以通过指定 `matrix` 的 `dtype` 参数来定义,例如下面的三种方式都是产生一个 `int32` 型的标量:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "x = T.scalar('myvar', dtype='int32')\n", + "x = T.iscalar('myvar')\n", + "x = T.TensorType(dtype='int32', broadcastable=())('myvar')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "常用的构造函数有:\n", + "\n", + "- `T.scalar(name=None, dtype=config.floatX)`\n", + "- `T.vector(name=None, dtype=config.floatX)`\n", + "- `T.row(name=None, dtype=config.floatX)`\n", + "- `T.col(name=None, dtype=config.floatX)`\n", + "- `T.matrix(name=None, dtype=config.floatX)`\n", + "- `T.tensor3(name=None, dtype=config.floatX)`\n", + "- `T.tensor4(name=None, dtype=config.floatX)`\n", + "\n", + "还可以使用一个构造多个变量:\n", + "- `T.scalars`\n", + "- `T.vectors`\n", + "- `T.rows`\n", + "- `T.cols`\n", + "- `T.matrices`\n", + "\n", + "除此之外,我们还可以用 `TensorType` 类自定义的符号变量:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "dtensor5 = T.TensorType('float64', (False,)*5)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}