{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 可变和不可变类型" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 列表是可变的(Mutable)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1,2,3,4]\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "通过索引改变:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[100, 2, 3, 4]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0] = 100\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "通过方法改变:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[100, 2, 3, 200, 4]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.insert(3, 200)\n", "a" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4, 100, 200]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sort()\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 字符串是不可变的(Immutable)" ] }, { "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": "markdown", "metadata": {}, "source": [ "通过索引改变会报错:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "scrolled": true }, "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[1;32m----> 1\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'z'\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[0] = 'z'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符串方法只是返回一个新字符串,并不改变原来的值:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello Mars\n", "hello world\n" ] } ], "source": [ "print s.replace('world', 'Mars')\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果想改变字符串的值,可以用重新赋值的方法:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello Mars\n" ] } ], "source": [ "s = \"hello world\"\n", "s = s.replace('world', 'Mars')\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "或者用 `bytearray` 代替字符串:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bytearray(b'a12de')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = bytearray('abcde')\n", "s[1:3] = '12'\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "数据类型分类:\n", "\n", "|可变数据类型|不可变数据类型|\n", "|--|--|\n", "|`list`, `dictionary`, `set`, `numpy array`, `user defined objects`|`integer`, `float`, `long`, `complex`, `string`, `tuple`, `frozenset`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 字符串不可变的原因" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "其一,列表可以通过以下的方法改变,而字符串不支持这样的变化。" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = [1, 2, 3, 4]\n", "b = a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "此时, `a` 和 `b` 指向同一块区域,改变 `b` 的值, `a` 也会同时改变:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[100, 2, 3, 4]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[0] = 100\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "其二,是字符串与整数浮点数一样被认为是基本类型,而基本类型在Python中是不可变的。" ] } ], "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 }