{ "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 }