{ "metadata": { "name": "list-tuple-dict" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# \u5217\u8868(list), \u5143\u7ec4(tuple)\u548c\u5b57\u5178(dict)\n", "\n", "## \u5217\u8868" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1, 2, 3, 4, 5, 6, 7]\n", "print 'a is', a\n", "\n", "print 'element in position 2 is', a[2]\n", "print 'the position of 3 is', a.index(3)\n", "print 'element in position 1~3 are', a[1:4] # half open range\n", "print 'element from position 2 are', a[2:]\n", "print 'element to last but one are', a[:-1]\n", "print 'elements in even position', a[::2]\n", "print 'elements in event position within a range', a[1:-1:2]\n", "\n", "a[0] = 0\n", "print 'after change element in position 0:', a\n", "\n", "a.append(2)\n", "print 'after append:', a\n", "a.extend([4, 'five'])\n", "print 'after extent:', a\n", "a.remove(2)\n", "print 'after remove element 2:', a\n", "a.pop()\n", "print 'after pop:', a\n", "print 'is 2 in a:', 2 in a\n", "\n", "del a[2]\n", "print 'after delete element in position 2:', a\n", "print 'is 2 in a:', 2 in a" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a is [1, 2, 3, 4, 5, 6, 7]\n", "element in position 2 is 3\n", "the position of 3 is 2\n", "element in position 1~3 are [2, 3, 4]\n", "element from position 2 are [3, 4, 5, 6, 7]\n", "element to last but one are [1, 2, 3, 4, 5, 6]\n", "elements in even position [1, 3, 5, 7]\n", "elements in event position within a range [2, 4, 6]\n", "after change element in position 0: [0, 2, 3, 4, 5, 6, 7]\n", "after append: [0, 2, 3, 4, 5, 6, 7, 2]\n", "after extent: [0, 2, 3, 4, 5, 6, 7, 2, 4, 'five']\n", "after remove element 2: [0, 3, 4, 5, 6, 7, 2, 4, 'five']\n", "after pop: [0, 3, 4, 5, 6, 7, 2, 4]\n", "is 2 in a: True\n", "after delete element in position 2: [0, 3, 5, 6, 7, 2, 4]\n", "is 2 in a: True\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## \u5143\u7ec4\n", "\n", "\u5143\u7ec4\u4e0e\u5217\u8868\u7c7b\u4f3c, \u53ef\u4ee5:\n", "\n", "* \u8bbf\u95ee\u5143\u7d20(`a[2]`)\n", "* \u5b9a\u4f4d(`index`)\n", "* \u68c0\u67e5\u5143\u7d20\u5b58\u5728\u4e0e\u5426(`in`)\n", "* \u5207\u7247(`a[2:-1]`)\n", "\n", "\u4f46\u662f\u5143\u7ec4\u662f\u4e0d\u53ef\u4fee\u6539\u7684, \u4e0d\u53ef\u4ee5:\n", "\n", "* \u66f4\u6539\u5143\u7d20(`a[2] = 3`)\n", "* `append`\n", "* `extent`\n", "* `remove`\n", "* `pop`\n", "* `del`\n", "\n", "\u5143\u7ec4\u53ef\u4ee5\u4fdd\u5b58\u4e0d\u53d8\u7684\u6570\u636e, \u6bd4\u5217\u8868\u6548\u7387\u66f4\u9ad8." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = (1, 2, 3, 4, 5, 6, 7)\n", "print 'a is', a\n", "print 'is 2 in a:', 2 in a\n", "print 'the position of 7 is:', a.index(7)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a is (1, 2, 3, 4, 5, 6, 7)\n", "is 2 in a: True\n", "the position of 7 is: 6\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## \u5b57\u5178\n", "\n", "\u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u79cd\"\u952e(key)\"\u548c\"\u503c(value)\"\u4e4b\u95f4\u7684\u591a\u5bf9\u4e00\u6620\u5c04." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Image, display\n", "\n", "d = {\n", " 'neta': '\u634f\u4ed6',\n", " 42: 'the answer to life the universe and everything'\n", "}\n", "\n", "print '%d is %s' % (42, d[42])\n", "print 'neta means %s' % d['neta'], 'not \u634f\u5979'\n", "display(Image(url='http://goo.gl/tVTG5'))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "42 is the answer to life the universe and everything\n", "neta means \u634f\u4ed6 not \u634f\u5979\n" ] }, { "html": [ "" ], "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "\u5b57\u5178\u63d0\u4f9b\u4e0e\u5217\u8868\u7c7b\u4f3c\u7684`del`\u548c`in`\u64cd\u4f5c:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'foo': 'bar', 1: -1, 'bar': 'foo'}\n", "\n", "print d\n", "del d['foo']\n", "print d\n", "print 1 in d" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{1: -1, 'foo': 'bar', 'bar': 'foo'}\n", "{1: -1, 'bar': 'foo'}\n", "True\n" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }