Python 入门(四)List和Tuple类型

时间:2021-07-02 17:59:29

创建list

Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。

比如,列出班里所有同学的名字,就可以用一个list表示:

>>> ['Michael', 'Bob', 'Tracy']
['Michael', 'Bob', 'Tracy']

list是数学意义上的有序集合,也就是说,list中的元素是按照顺序排列的。

构造list非常简单,按照上面的代码,直接用 [ ] 把list的所有元素都括起来,就是一个list对象。通常,我们会把list赋值给一个变量,这样,就可以通过变量来引用list:

>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates # 打印classmates变量的内容
['Michael', 'Bob', 'Tracy']

由于Python是动态语言,所以list中包含的元素并不要求都必须是同一种数据类型,我们完全可以在list中包含各种数据:

>>> L = ['Michael', 100, True]

一个元素也没有的list,就是空list:

>>> empty_list = []

任务

假设班里有3名同学:Adam,Lisa和Bart,他们的成绩分别是 95.5,85 和 59,请按照 名字, 分数, 名字, 分数... 的顺序按照分数从高到低用一个list表示,然后打印出来。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAy8AAABuCAIAAACho7szAAAOoUlEQVR4nO2dwWvcZhqH54/Yyx68wYdCWUwOC4GQiRMaDMY2uQTqZU3tlLaE7KUxOw6UhhJSSAILvqxdYpuQk/GpIcSQ7mEmCz3Y+K+YP6CntCHn2YNG0qeRPo2+0ffpfTN+Hp7DWKORPr1WXv36SZ62/vTnvyAiIiKilC3xESAiIiKeZ0ljiIiIiJKSxhARERElJY0hIiIiSkoaQ0RERJSUNIaIiIgoKWkMERERUVLSGCIiIqKkpDFERERESUljiIiIiJKSxhARERElJY0hIiIiStp69+7du3fvfv/99z/++OO3336buTCLvmwBAFgYAADEkMZIYwAggHTzBwBFkMZIYwAggHTzBwBFkMZIYwAggHTzBwBFkMZIYwAggHTzBwBFkMZIYwAggHTzBwBFkMZIYwAggHTzBwBFkMZIYwAggHTzBwBFtN6/f08aI40BQMNIN38AUETrw4cP79+/J42RxgCgSaSbPwAoovXLL798+PCBN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ogjSEiCijd/AFAEaQxREQBpZs/ACiCNIaIKKB08wcARZDGEBEFlG7+AKAI0hgiooDSzR8AFEEaQ0QUULr5A4AiSGOIiAJKN38AUARpDBFRQOnmDwCK+D8BjrudEnrUkgAAAABJRU5ErkJggg==" alt="" />

按照索引访问list

由于list是一个有序集合,所以,我们可以用一个list按分数从高到低表示出班里的3个同学:

>>> L = ['Adam', 'Lisa', 'Bart']

那我们如何从list中获取指定第 N 名的同学呢?方法是通过索引来获取list中的指定元素。

需要特别注意的是,索引从 0 开始,也就是说,第一个元素的索引是0,第二个元素的索引是1,以此类推。

因此,要打印第一名同学的名字,用 L[0]:

>>> print L[0]
Adam

要打印第二名同学的名字,用 L[1]:

>>> print L[1]
Lisa

要打印第三名同学的名字,用 L[2]:

>>> print L[2]
Bart

要打印第四名同学的名字,用 L[3]:

>>> print L[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

报错了!IndexError意思就是索引超出了范围,因为上面的list只有3个元素,有效的索引是 0,1,2。

所以,使用索引时,千万注意不要越界

任务

三名同学的成绩可以用一个list表示:

L = [95.5, 85, 59]

请按照索引分别打印出第一名、第二名、第三名、第四名的分数。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiAAAACXCAIAAACju3evAAANiElEQVR4nO2dTWvbWgKG8yNmMwtP8KJQhjCLgUDoJzcEgmO6bC5TmqTclsBsihm3cLnhkkmhWQzczcQlH3QZZnVDSDazsBnIwiG/Qn/Fs5D1ZUu2JR+dV7Keh2cRO7Jkp+Z9Kx1JZ+kPf/wTIiKicZfk7wARERdSCgYREXORgkFExFykYBARMRcpGEREzEUKBhERc5GCQUTEXKRgEBExFykYRETMRQoGERFzkYJBRMRcXKot19GUSwAACQyqBwVDwQCADdRpL4CCoWAAwAbqtBdAwVAwAGADddoLoGAoGACwgTrtBVAwFAwA2ECd9gIoGAoGAGygTnsBFAwFAwA2UKe9AAqGggEAG6jTXgAFQ8EAgA3UaS+AgqFgAMAG6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V9YiQFk/OtYtYuvass73ZiPh2nKSNWWXXaCzBSMI2Dm/vOWbobkUkOkeVaMPutvctGvbZcr62u351sH6+OvoqCQayy6rQXYKBgNg6v+zeHGynvdDlWMGWfcCzsyvFRzE4MBYNYZdVpL2Dugtk8vHq4PthMfSvl2IJZhAnHluvusbLh3kxICgaxyqrTXsC8BRNMk2xmD8Z/WNoJx9y7Xh6tb4w9T8EgVll12guYr2DCpZJrwZRnPpiNnW0nbgCmRsEgVlt12guYq2D8yZLDdt6bKJhyTjg2oV1qFAxitVWnvQBz18EY3YMp5YRjjeaEdqlRMIjVVp32AopVMGWecCy4CIYJxxBxXHXaCyjOlfxlu10/88EgYhrVaS+AgskiBYOIaVWnvQAKJovciwwR06pOewHFKRhExEVWnfYClh7/+S8q5f/eFnzy7PmTZ8/lbwMR5arTXgAFk68UDCK6qtNeAAWTrxQMIrqq015A2Qum6BOOPXn2/MmzHxnkR0R12guYt2A2D6/DVyNeHTaLXDB5zwfjjE04Ft6D4TRlxCqrTnsBBgomValoD5HZv10/BYOIruq0F1CcglmkCcfq+63hTgwFg4iu6rQXUKyCWZAJx1bX77wbkVEwiOiqTnsBRsdgbv65Oe8ejP+wlBOO+cMwjMEg4ojqtBdg8iyyn87SdUz0r78gE4657rf2nNZajYJBRE912gswepryh9P+w+lPRgqmnBOOBTaa7qzJFAwiuqrTXoDBgmn+enPfP9s3coislBOOBa4cH7EHg4gR1WkvYM6Caf56k/EimNiCKfOEYyvHR1wHg4iJqtNeQHGu5C/b7fpnmw+GgkFEV3XaC6BgskjBIGJa1WkvgILJIvciQ8S0qtNeQHEKZjHlbsqI6KpOewHMaImIaEN12gugYBARbahOewEUDCKiDdVpL4CCQUS0oTrtBfwfpuqaWOXOBtcAAAAASUVORK5CYII=" alt="" />

倒序访问list

我们还是用一个list按分数从高到低表示出班里的3个同学:

>>> L = ['Adam', 'Lisa', 'Bart']

这时,老师说,请分数最低的同学站出来。

要写代码完成这个任务,我们可以先数一数这个 list,发现它包含3个元素,因此,最后一个元素的索引是2:

>>> print L[2]
Bart

有没有更简单的方法?

有!

Bart同学是最后一名,俗称倒数第一,所以,我们可以用 -1 这个索引来表示最后一个元素:

>>> print L[-1]
Bart

Bart同学表示躺枪。

类似的,倒数第二用 -2 表示,倒数第三用 -3 表示,倒数第四用 -4 表示:

>>> print L[-2]
Lisa
>>> print L[-3]
Adam
>>> print L[-4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

L[-4] 报错了,因为倒数第四不存在,一共只有3个元素。

使用倒序索引时,也要注意不要越界

任务

三名同学的成绩可以用一个list表示:

L = [95.5, 85, 59]

请按照倒序索引分别打印出倒数第一、倒数第二、倒数第三、倒数第四的分数。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkEAAACnCAIAAACZ5B9MAAAOk0lEQVR4nO2dzWsbZx6A9UfsZQ+K0aFQFncPCwKTpgkVWowscoyXTeMPmmDYSxFVQz9M0boQwS7tZa1g2SQ3s6c6xj4kB4ktPkjor5g/oJe0CTprD5LmQ9JIGumd+ek38zw8FEuWZlQleR/m80394Y+3EBERNZoS/wSIiIiLScMQEVGrNAwREbVKwxARUas0DBERtUrDEBFRqzQMERG1SsMQEVGrNAwREbVKwxARUas0DBERtUrDEBFRqzQMERG1SsMQEVGrNAwREbVKwxARUas0DBERtUrDEBFRq6m3b9/+9ttv/f/a/P777+/evfv111/Taxk0ZQoAwIceLAQNo2EAII90C7RCw2gYAMgj3QKt0DAaBgDySLdAKzSMhgGAPNIt0AoNo2EAII90C7SSevv27fv377vd7vv372kYDQMAEaRboJXUu3fvut3uRx995M4YDaNhABAl0i3QSqrb7d66dSubzX722Wd2xmgYDQOAKJFugVZSmUymUCh8++23P/74Y7Va7WeMhtEwAIgS6RZoJfX111+/fPny1atXb968+eWXX16/ft3tdmkYDQOAKJFugVZS3THYDqNhABAx0i3QCufW0zAAkMfYmD51yWbXtQrQMBoGAPIYG9P9F2X/yuDqxKFhNAwA5DE2pvssKjW2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74sxUAAAAASUVORK5CYII=" alt="" />

添加新元素

现在,班里有3名同学:

>>> L = ['Adam', 'Lisa', 'Bart']

今天,班里转来一名新同学 Paul,如何把新同学添加到现有的 list 中呢?

第一个办法是用 list 的 append() 方法,把新同学追加到 list 的末尾:

>>> L = ['Adam', 'Lisa', 'Bart']
>>> L.append('Paul')
>>> print L
['Adam', 'Lisa', 'Bart', 'Paul']

append()总是把新的元素添加到 list 的尾部。

如果 Paul 同学表示自己总是考满分,要求添加到第一的位置,怎么办?

方法是用list的 insert()方法,它接受两个参数,第一个参数是索引号,第二个参数是待添加的新元素:

>>> L = ['Adam', 'Lisa', 'Bart']
>>> L.insert(0, 'Paul')
>>> print L
['Paul', 'Adam', 'Lisa', 'Bart']

L.insert(0, 'Paul') 的意思是,'Paul'将被添加到索引为 0 的位置上(也就是第一个),而原来索引为 0 的Adam同学,以及后面的所有同学,都自动向后移动一位。

任务

假设新来一名学生Paul,Paul 同学的成绩比Bart好,但是比Lisa差,他应该排到第三名的位置,请用代码实现。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAv8AAAB8CAIAAABxIkgqAAAPS0lEQVR4nO2dT2vcZh7H/SL2sodZM4dCKMWHhUDp1AkxBdc2uZitlzWxE9oQspc47DgQYkraLKlhwZfExn8IPgWfYowNYQ8zWejBZl7FvIDe2jegPWgkPY+kRxqNHj0/eebz4XOYzIykn2bcn7599Iw09ac//wURERFxcpwSrwARERHRpaQfREREnCxJP4iIiDhZkn4QERFxsiT9ICIi4mRJ+kFERMTJkvSDiIiIkyXpBxERESdL0g8iIiJOlqQfREREnCynfv/99z/++OO3335rTDfRllMAAAY8AJCG9EP6AQCnSLd9ACD9kH4AwC3SbR8ASD+kHwBwi3TbBwDSD+kHANwi3fYBgPRD+gEAt0i3fQAg/ZB+AMAt0m0fAEg/pB8AcIt02wcA0g/pBwDcIt32AYD0Q/oBALdIt30AIP2QfgDALZabeGLNVWxl+Erqsx6p7VK/3e1W9N8O6Yf0AwBOsd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F+EFFK6bYPAJWd+bo8eFhJ+jGfpSL9IOK1ULrtA0BFv3j/Yf/y/OU3VtLPzc13uzvfTTcbpB9EHAul2z4AVJJ+FrbOrY39fPc0mpVsMf3oJ9HyJP0goj2l2z4AWEs/C1vntq73M5j1nPxBliH96O+PFslKP9pSw/zmqxftHUkIEcso3fYBoI7Xeo7N+0FEHCul2z4AkH4QEd0q3fYBgPSDiOhW6bYPAHVMP4iI46x02wcA0g8iolul2z4AkH4QEd0q3fYBgPSDiOhW6bYPAKQfRES3Srd9ACD9ICK6VbrtAwDpBxHRrdJtHwBIP4iIbpVu+wBA+kFEdKt02wcA0g8iolul2z4AkH4QEd0q3fYBgPSDiOhW6bYPAKQfRES3Srd9APD+Dwy4lnHRopyyAAAAAElFTkSuQmCC" alt="" />

从list删除元素

Paul同学刚来几天又要转走了,那么我们怎么把Paul 从现有的list中删除呢?

如果Paul同学排在最后一个,我们可以用list的pop()方法删除:

>>> L = ['Adam', 'Lisa', 'Bart', 'Paul']
>>> L.pop()
'Paul'
>>> print L
['Adam', 'Lisa', 'Bart']

pop()方法总是删掉list的最后一个元素,并且它还返回这个元素,所以我们执行 L.pop() 后,会打印出 'Paul'。

如果Paul同学不是排在最后一个怎么办?比如Paul同学排在第三:

>>> L = ['Adam', 'Lisa', 'Paul', 'Bart']

要把Paul踢出list,我们就必须先定位Paul的位置。由于Paul的索引是2,因此,用 pop(2)把Paul删掉:

>>> L.pop(2)
'Paul'
>>> print L
['Adam', 'Lisa', 'Bart']

任务

注意右边编辑器代码中 list 如下:

L = ['Adam', 'Lisa', 'Paul', 'Bart']

Paul的索引是2,Bart的索引是3,如果我们要把Paul和Bart都删掉,请解释下面的代码为什么不能正确运行:

L.pop(2)
L.pop(3)

怎样调整代码可以把Paul和Bart都正确删除掉?

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnsAAACMCAIAAACLXvhIAAAO90lEQVR4nO2dT2vbWhqH8yFmMwtPyKJQhtLFQOFSNy0NAU8Suinc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RENKd0qQcVFE/ctd3zXrN1KJG42SRxEVFK6VIPKiiauKt7Z93zvdUXBRP3wQ+/tj+sb31oti+a7Yvmtx+8CxpX69++1B9U698umu2LZhCZ/jMXb7cmridY2DfyrjhJXEQ0p3SpBxUUS9za3mn/bLd264aJxL1otj+sL1XcgPTGiodh6f54Z+dLKIyHr44lbiRoQ6/Sx0VEKaVLPaigUOI+a/VO99Zu3DSTuG7cVhaXKotbH8KpGRypDZ53jU3cYD3r++GjvCQuIkopXepBBQUSN5yyVhM3eQSYxEXEmVC61IMK8ifus1Yvcn+9fq/b7zVfmEjcOztfmvvfLy5VSFxEnAulSz2owND5uEb7uN+/DWY2GUzc6AD1JElcRDSndKkHFShK3NiJxAmJG10+eEta4kbeNc1c5XD3nfRFxAJKl3pQgZJrTo0cx0VEnCulSz2ogMRFRLSudKkHFZC4iIjWlS71oAIliYuIOM9Kl3pQAYmLiGhd6VIPKiBxERGtK13qQQUkLiKidaVLPaiAxEVEtK50qQcVkLiIiNaVLvWgAhIXEdG60qUeVEDiIiJaV7rUgwpIXERE60qXelABiYuIaF3pUg8qIHEREa0rXepBBSQuIqJ1pUs9qIDERUS0rnSpBxX8Hynoncz9wIhjAAAAAElFTkSuQmCC" alt="" />

替换元素

假设现在班里仍然是3名同学:

>>> L = ['Adam', 'Lisa', 'Bart']

现在,Bart同学要转学走了,碰巧来了一个Paul同学,要更新班级成员名单,我们可以先把Bart删掉,再把Paul添加进来。

另一个办法是直接用Paul把Bart给替换掉:

>>> L[2] = 'Paul'
>>> print L
L = ['Adam', 'Lisa', 'Paul']

对list中的某一个索引赋值,就可以直接用新的元素替换掉原来的元素,list包含的元素个数保持不变。

由于Bart还可以用 -1 做索引,因此,下面的代码也可以完成同样的替换工作:

>>> L[-1] = 'Paul'

任务

班里的同学按照分数排名是这样的:

L = ['Adam', 'Lisa', 'Bart']

但是,在一次考试后,Bart同学意外取得第一,而Adam同学考了倒数第一。

请通过对list的索引赋值,生成新的排名。

aaarticlea/png;base64,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ztzhf5jJmLErZXdq8O5rFP1a4cqqgnAeTowe92XMKqvNsSIQHrwvcpoBCJqqV0FAETYihHn1w6254eOHTcTI5wMke3LZfuKV/1RwJv94C13bBojvHaGl/wzJ4gRiKildhUAEGEnRvijg9UYEX4AghiBiF2hdhUAEGElRpxfOwjc2rt8UCofrFwwESMGZq+vLD3bl8sSIxCxJ9SuAgAi7F83wuhoxLPT3hRIgzEieHyklcQIRDSndhUAENEdMaLpaRQhMSL4fu8jUTEi8Kl2ztTwD7QQKRBRoHYVABDR+VexrJsbgYjYU2pXAQARxAhERE21qwCACGIEIqKm2lUAQETnxwhExF5WuwoAiCBGICJqql0FAEQQIxARNdWuAgAiiBGIiJpqVwEAEcQIRERNtasAgAhiBCKiptpVAEAEMQIRUVPtKgAgghiBiKipdhUAEEGMQETUVLsKAIggRiAiaqpdBQBEECMQETXVrgIAIogRiIiaalcBABHECERETbWrAIAIYgQioqbaVQBABDECEVFT7SoAIIIYgYioqXYVABBBjEBE1FS7CgCIIEYgImqqXQUARBAjEBE11a4CACKIEYiImmpXAQARxAhERE21qwCAiP8DnhZWuAjzRg0AAAAASUVORK5CYII=" alt="" />

创建tuple

tuple是另一种有序的列表,中文翻译为“ 元组 ”。tuple 和 list 非常类似,但是,tuple一旦创建完毕,就不能修改了。

同样是表示班里同学的名称,用tuple表示如下:

>>> t = ('Adam', 'Lisa', 'Bart')

创建tuple和创建list唯一不同之处是用( )替代了[ ]

现在,这个 t 就不能改变了,tuple没有 append()方法,也没有insert()和pop()方法。所以,新同学没法直接往 tuple 中添加,老同学想退出 tuple 也不行。

获取 tuple 元素的方式和 list 是一模一样的,我们可以正常使用 t[0],t[-1]等索引方式访问元素,但是不能赋值成别的元素,不信可以试试:

>>> t[0] = 'Paul'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

任务

创建一个tuple,顺序包含0 - 9这10个数。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAusAAAB1CAIA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kXC+qvd6gEMcrvek3faP9BXds+mepM0tlfZnveDwfqr3eoBDHK7Ekzme/Jm+/j990mXYNi+RtvznrxYf7VbPYBBbluC4f8iGZT/i4T1V7vVAxjk9iUYNCf/mxrrr3arBzBI687dRRXVGwoi4szUbvUABiHBICKKq93qAQxCgkFEFFe71QMYpNEJ5tGbT/2LT2/va/cmRMTJard6AINUTDCve+Hb2Z19XKt5gvl1u/+t86h8fd7dFRFdqd3qAQxSKcG8PLrq/b545+7inV8+nl2f7/5S66tIJBhE1FK71QMYxNVVpLXdH9NNw6R/vR+9+dT/vP7sc69/0etf9E7fhO8uf79z+q3z6H7n9KLXv+jFESR65GL7WWGdeOPI1F6jruyepf9bgsx/OUDEW6N2qwcwiKsE87p3Hc7H3DTBXPT6n9cX5vzAEV4bCsKH/+nS22+JcBN8dSzBpIJL4qvMwSCiltqtHsAgbhLMy6Oryx8fn0yzS/rX2587iT599jmZQuKVLvHjvpkJJq6z/kdylQwJBhG11G71AAZxkGCefDy/nHIRzHQJJv+KDwkGERuhdqsHMEjVBHOz+FKQYJbefuv98ev8whwJBhFNqN3qAQxSLcH8fniz+DI5wfy6Ha+0dZhg0hekil3ZPbvkf0Aiogu1Wz2AQaokmPjNYEIPX1ZIMJk3CuUkmPT28S6TEkxqr4n3IoWu7pxzLxIiOlC71QMYpCbvyTuyDgYR0ZTarR7AICQYRERxtVs9gEFIMIiI4mq3egCD1CTBICJaVrvVAxiEBIOIKK52qwcwCAkGEVFc7VYPYBASDCKiuNqtHsAgJBhERHG1Wz2AQUgwiIjiard6AIOQYBARxdVu9QAGIcEgIoqr3eoBDEKCQUQUV7vVAxiEBIOIKK52qwcwCAkGEVFc7VYPYBASDCKiuNqtHsAgJBhERHG1Wz2AQUgwiIjiard6AIOQYBARxdVu9QAGIcEgIoqr3eoBDEKCQUQUV7vVAxjk/1m/JkHqM5E6AAAAAElFTkSuQmCC" alt="" />

补充说明:如果采用range(0,10)函数产生的是序列,而非元组;

aaarticlea/png;base64,iVB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1CLavR3ACjJLTD6DSZCYuRdf2r/fnZ6ZQmIIIRMR7d4OYAUZJWbtcz6DiZeYu+ujE28FJcZ/ZCo5C5uHp2X9NCMhZLKj3dsBrCCTxIy+JGaYz48MJCb00qEIifE/f7RKnMT41oq9OmmY5usjrk4ihAhEu7cDWIHWN/YGzokhhJCJinZvB7ACJIYQQuSj3dsBrACJIYQQ+Wj3dgArqNcPQBJCSD2i3dsBrACJIYQQ+Wj3dgArQGIIIUQ+2r0dwAqQGEIIkY92bwewAiSGEELko93bAawAiSGEEPlo93YAK0BiCCFEPtq9HcAKkBhCCJGPdm8HsAIkhhBC5KPd2wGs4P+feUEDF/Qk/QAAAABJRU5ErkJggg==" alt="" />

先列几个range()函数的几个用法

Python 入门(四)List和Tuple类型

函数原型:range(start, end, scan):

参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);

end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

创建单元素tuple

tuple和list一样,可以包含 0 个、1个和任意多个元素。

包含多个元素的 tuple,前面我们已经创建过了。

包含 0 个元素的 tuple,也就是空tuple,直接用 ()表示:

>>> t = ()
>>> print t
()

创建包含1个元素的 tuple 呢?来试试:

>>> t = (1)
>>> print t
1

好像哪里不对!t 不是 tuple ,而是整数1。为什么呢?

因为()既可以表示tuple,又可以作为括号表示运算时的优先级,结果 (1) 被Python解释器计算出结果 1,导致我们得到的不是tuple,而是整数 1。

正是因为用()定义单元素的tuple有歧义,所以 Python 规定,单元素 tuple 要多加一个逗号“,”,这样就避免了歧义:

>>> t = (1,)
>>> print t
(1,)

Python在打印单元素tuple时,也自动添加了一个“,”,为了更明确地告诉你这是一个tuple。

多元素 tuple 加不加这个额外的“,”效果是一样的:

>>> t = (1, 2, 3,)
>>> print t
(1, 2, 3)

任务

请指出右边编辑器中代码为什么没有创建出包含一个学生的 tuple:

t = ('Adam')

print t

请修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95yH7cX79rf607tvvygfekPE/Ks97Q2RNGyvj2+bfz24d//BveX98+7F3nKuD0USNkTUUnvaGyLFQ5G/7/2Mt2gL/6s/ffep/Xml9rnZvmy2L5vf3z0a3FStf/9Wf1qtf79sti+bfpm8ay7f16Zux7+zZ+hRwwr94VNENKP2tDdEimF72+y6q7dZw3bZbH9eWZhzOuQeYBw0yfny0fa3QPMGt46ELdSzwK2s2BBRS+1pb4jUwvb6+Pbm5/5vcR4S/ld3Vlrel7XPwTj576L51zuODZu/nZXD4DtwhA0RtdSe9oZIJ2y/7V/cxHyDLV7Yog8bEjZELITa094QKYRttqpNCduj7W/NwxfzC3OEDRFLofa0N0TisP11NFvVJoftxXv/5I4UwxY+qjndrP/wKSLaUXvaGyJh2PwPsXl/aPR1grCNPWUxImzh+/sPmRS20KMmnhXpmvEfPkVEM2pPe0Pk5zePDL3HhohYKrWnvSEIGyKihNrT3hCEDRFRQu1pb4j8hA0RscxqT3tDEDZERAm1p70hCBsiooTa094QhA0RUULtaW8IwoaIKKH2tDcEYUNElFB72huCsCEiSqg97Q1B2BARJdSe9oYgbIiIEmpPe0MQNkRECbWnvSEIGyKihNrT3hCEDRFRQu1pbwjChogoofa0NwRhQ0SUUHvaG4KwISJKqD3tDUHYEBEl1J72hiBsiIgSak97QxA2REQJtae9IQgbIqKE2tPeEIQNEVFC7WlvCMKGiCih9rQ3BGFDRJRQe9obgrAhIkqoPe0NQdgQESXUnvaGIGyIiBJqT3tDEDZERAm1p70hCBsiooTa094QhA0RUULtaW+I/wPAyGNm4m14RAAAAABJRU5ErkJggg==" alt="" />

“可变”的tuple

前面我们看到了tuple一旦创建就不能修改。现在,我们来看一个“可变”的tuple:

>>> t = ('a', 'b', ['A', 'B'])

注意到 t 有 3 个元素:'a','b'和一个list:['A', 'B']。list作为一个整体是tuple的第3个元素。list对象可以通过 t[2] 拿到:

>>> L = t[2]

然后,我们把list的两个元素改一改:

>>> L[0] = 'X'
>>> L[1] = 'Y'

再看看tuple的内容:

>>> print t
('a', 'b', ['X', 'Y'])

不是说tuple一旦定义后就不可变了吗?怎么现在又变了?

别急,我们先看看定义的时候tuple包含的3个元素:

Python 入门(四)List和Tuple类型

当我们把list的元素'A'和'B'修改为'X'和'Y'后,tuple变为:

Python 入门(四)List和Tuple类型

表面上看,tuple的元素确实变了,但其实变的不是 tuple 的元素,而是list的元素。

tuple一开始指向的list并没有改成别的list,所以,tuple所谓的“不变”是说,tuple的每个元素,指向永远不变。即指向'a',就不能改成指向'b',指向一个list,就不能改成指向其他对象,但指向的这个list本身是可变的!

理解了“指向不变”后,要创建一个内容也不变的tuple怎么做?那就必须保证tuple的每一个元素本身也不能变。

任务

定义了tuple:

t = ('a', 'b', ['A', 'B'])

由于 t 包含一个list元素,导致tuple的内容是可变的。能否修改上述代码,让tuple内容不可变?

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArsAAABwCAIAAACRsS+LAAAMEUlEQVR4nO2dT2vbahaH/SFmMwtPyKJQhpLFQOGCaUrDhXATMotCCxMaU3q76GyaMk4gJJRAoRQGsiku+UPJqmTVYBoos7Dv7BzyKfxFZuFZ2JIlWZIl6xwdWXoenoUjy3pfvU7O+7MkK7U//fkviIiIiPHWzHuAiIiIxZfEgIiIiLMlMSAiIuJsSQyIiIg4WxIDIiIizrZWX1pGEWsAABEMARYfEgOJAQDUsS71AAKQGEgMAKCOdakHEIDEQGIAAHWsSz2AACQGEgMAqGNd6gEEIDGQGABAHetSDyAAiYHEAADqWJd6AAFIDCQGAFDHutQDCEBiIDEAgDrWpR5AABIDiQEA1LEu9QACkBhIDACgjlYFd7acthWpXlm1O71Z2/5ItRu1Eb1foVSQGEgMAKCOSvn2bDZtK1K9smo3sE3z/gi2m3Z5npAYSAwAoI587Z7aZtpWpHoV2pOYjcuORqpxCH1KexyifgfmGCKNX6RUkBhIDACgjnztLvYH69DlGkORahwCz+Y8Du7jtP2cuTw3SAwkBgBQR7hwW88cM8mnh2lb0XgvZrYY+jjVC5MszwcSA4k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