《零基礎入門學習Python》(13)--元組:戴上了枷鎖的列表
阿新 • • 發佈:2018-12-14
前言
這節課我們討論主題是元祖:我們有個小標題戴上了枷鎖的列表
列表同志真是千古功名一聚塵吶。自從列表同志被抓了後,Python的作者感覺有點大事不妙了。可能列表給的權利太大了。
我們應該做一些限制,所以呢,Python的作者就Python社群的小夥伴們,推出了我們今天的主題:元組(tuple)
如果你定義了一個元組,那麼裡面的任何一個元素無法改變了。
知識點
由於和列表是近親關係,所以元組和列表在實際使用上是非常相似的。
我們這節課主要通過討論元組合列表到底有什麼不同來學習元組,醬紫大家就不會覺。老是重複一樣的內容
我們主要從以下幾個點來討論學習:
首先我們強調一下,元組和列表最大的區別就是列表可以隨意修改裡面的元素,而元組就不能
建立和訪問一個元祖
- 建立列表時,是用的中括號[],而建立元組時,大部分用的是小括號()
>>> tuple1 = (1,2,3,4,5,6,7,8)
>>> tuple1
(1, 2, 3, 4, 5, 6, 7, 8)
- 使用元組——使用方法和列表類似(以下分兩種方法使用一種是索引一種是切片)
>> tuple1[1] 2 >>> tuple1[5:] (6, 7, 8) >>> tuple1[:5] (1, 2, 3, 4, 5) >>> tuple2 = tuple1[:] >>> tuple2 (1, 2, 3, 4, 5, 6, 7, 8)
- 讓我們嘗試更改元組的值
>>> tuple1[1] = 3
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
tuple1[1] = 3
TypeError: 'tuple' object does not support item assignment
- 檢視一下元組元素的型別
>>> temp = (1)#當然它不是元組 >>> temp 1 >>> type(temp) <class 'int'> > temp2 = 2,3,4 >>> type(temp2) <class 'tuple'> >>> >>> temp = [] >>> type(temp) <class 'list'> >>> >>> temp = () >>> type(temp) <class 'tuple'> >>> >>> temp = (1,) >>> temp (1,) >>> type(temp) <class 'tuple'> >>> >>> temp = 1, >>> type(temp) <class 'tuple'> >>> temp (1,)
- 對元組使用操作符*
>>> 8 * (8)
64
>>> 8 * (8,)
(8, 8, 8, 8, 8, 8, 8, 8)
更新和刪除一個元組
>>> temp = ('小甲魚','黑夜','迷途','小布丁')
>>>
>>> temp = temp[:2] + ('怡靜',) + temp[2:]
>>> temp
('小甲魚', '黑夜', '怡靜', '迷途', '小布丁')
>>> temp = temp[:2] + temp[3:]
>>> temp
('小甲魚', '黑夜', '迷途', '小布丁')
>>> del temp
>>> temp
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
temp
NameError: name 'temp' is not defined
元組相關的一些操作符
- 拼接操作符
+
- 重複操作符
*
- 關係操作符
>
,<
,==
- 成員操作符
in
,not in
- 邏輯操作符
and
,or
課後習題
- 請用一句話描述什麼是列表?再用一句話描述什麼是元組? 列表:一個大倉庫,你可以隨時往裡邊新增或刪除任何東西。 元組:封閉的列表,一旦定義,就不可改變(不能新增,刪除或修改)
- 什麼情況下你需要使用元組而不是列表? 當我們希望內容不被輕易改寫時候,使用元組。 需要頻繁修改資料,使用列表
- 建立一個元組,什麼情況下逗號與小括號必須同時存在,缺一不可?
#在拼接只有一個元素的元組時候
>>> temp = (‘小甲魚’, ‘黑夜’, ‘迷途’, ‘小布丁’)
# 如果我想在“黑夜”和“迷途”之間插入“怡靜”,我們應該:
>>> temp = temp[:2] + (‘怡靜’,) + temp[2:]
x,y.z = 1,2,3 請問x,y,z是元組嗎?
#所有的多物件,逗號分隔,沒有明確用符號定義的這些集合預設型別都是元組
>>> x,y,z = 1,2,3
>>> type(x)
<class 'int'>
>>> type(x,y,z)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
type(x,y,z)
TypeError: type.__new__() argument 1 must be str, not int
>>> h = x,y,z
>>> type(h)
<class 'tuple'>
>>> h
(1, 2, 3)
- 請寫出以下情景中應該使用列表還是元組來儲存資料:
1)遊戲中角色的屬性: 列表
2)你的身份證資訊: 元組
3)論壇的會員: 列表
4)團隊合作開發程式,傳遞給一個你並不瞭解具體實現的函式的引數: 元組
5)航天火箭各個元件的具體配置引數: 元組
- 上節課我們通過課後作業的形式學習到了“列表推導式”,那請問如果把中括號改為小括號,會不會得到“元組推導式”呢?
>>> typle1 = (x**2 for x in range(10)) >>> typle1 <generator object <genexpr> at 0x00000065900D1200> >>> type(typle1) <class 'generator'> Python3 沒有“元組推導式”,那為什麼沒有報錯?因為你誤打誤撞得到了一個生成器: 嘗試訪問生成器: 注意:Python3開始next()方法改成位__next__()哦~ >>> typle1.__next__() 0 >>> typle1.__next__() 1 >>> typle1.__next__() 4 >>> typle1.__next__() 9 >>> typle1.__next__() 16 >>> typle1.__next__() 25 >>> typle1.__next__() 36 >>> typle1.__next__() 49 >>> typle1.__next__() 64 >>> typle1.__next__() 81 >>> typle1.__next__() Traceback (most recent call last): File "<pyshell#21>", line 1, in <module> typle1.__next__() StopIteration
6)NASA系統中記錄已經發現的行星資料:列表 --