{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 元组" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 基本操作" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "与列表相似,元组`Tuple`也是个有序序列,但是元组是不可变的,用`()`生成。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(10, 11, 12, 13, 14)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (10, 11, 12, 13, 14)\n", "t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以索引,切片:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[0]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(11, 12)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "但是元组是不可变的:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' 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[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[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "# 会报错\n", "t[0] = 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 单个元素的元组生成" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "由于`()`在表达式中被应用,只含有单个元素的元组容易和表达式混淆,所以采用下列方式定义只有一个元素的元组:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(10,)\n", "\n" ] } ], "source": [ "a = (10,)\n", "print a\n", "print type(a)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "a = (10)\n", "print type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "将列表转换为元组:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(10, 11, 12, 13, 14)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10, 11, 12, 13, 14]\n", "tuple(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 元组方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "由于元组是不可变的,所以只能有一些不可变的方法,例如计算元素个数 `count` 和元素位置 `index` ,用法与列表一样。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.count(10)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.index(12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 为什么需要元组" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "旧式字符串格式化中参数要用元组;\n", "\n", "在字典中当作键值;\n", "\n", "数据库的返回值……" ] } ], "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 }