可使用 index 的方式對 tuple 進行訪問,好比下面訪問 tuple 中的第二個元素。python
thistuple = ("apple", "banana", "cherry") print(thistuple[1]) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py banana
負數索引意味着從後往前計算,好比說: -1 表示最後一項, -2 表示倒數第二項,代碼以下:git
thistuple = ("apple", "banana", "cherry") print(thistuple[-1])
能夠指定一個範圍的 start 和 end 來指定一個 tuple 的 子區間,返回值就是一個新生成的 tuple,以下代碼所示:github
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(thistuple[2:5]) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py ('cherry', 'orange', 'kiwi')
若是你想從 tuple 的尾部往前進行切割,能夠指定負數的 index,以下代碼所示:markdown
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(thistuple[-4:-1]) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py ('orange', 'kiwi', 'melon')
檢查 tuple 中的某一項是否存在,可使用 in 關鍵詞。app
thistuple = ("apple", "banana", "cherry") if "apple" in thistuple: print("Yes, 'apple' is in the fruits tuple")
tuple 是不可修改的,意味着一旦 tuple 建立好以後,你不能對其進行新增,刪除,修改,但也有一些變通方法。ui
變通方法就是,能夠先將 tuple 轉成 list,而後在 list 上進行修改,最後再將 list 轉成 tuple 便可,以下代碼所示:this
x = ("apple", "banana", "cherry") y = list(x) y[1] = "kiwi" x = tuple(y) print(x) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py ('apple', 'kiwi', 'cherry')
一樣的道理,仍是使用 list 做爲中間轉換,實現 tuple 的 add / remove 操做,代碼以下:spa
thistuple = ("apple", "banana", "cherry") y = list(thistuple) y.append("orange") thistuple = tuple(y) print(thistuple) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py ('apple', 'banana', 'cherry', 'orange') thistuple = ("apple", "banana", "cherry") y = list(thistuple) y.remove("apple") thistuple = tuple(y) print(thistuple) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py ('banana', 'cherry')
如今有了一個 tuple,那如何將 tuple 中的值肢解到幾個變量中呢? 這就須要使用 unpacking 操做,代碼以下:code
fruits = ("apple", "banana", "cherry") (green, yellow, red) = fruits print(green) print(yellow) print(red) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py apple banana cherry
若是須要肢解的 tuple 個數大於左邊的變量個數,那麼能夠將一個變量聲明成 *
,表示將 多餘的 tuple 元素做爲一個集合賦給 *
號變量,若是不明白的話,參考下面代碼:blog
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry") (green, yellow, *red) = fruits print(green) print(yellow) print(red) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py apple banana ['cherry', 'strawberry', 'raspberry']
一樣這個 *號變量
也能夠指定任意位置,以下代碼所示:
fruits = ("apple", "mango", "papaya", "pineapple", "cherry") (green, *tropic, red) = fruits print(green) print(tropic) print(red) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py apple ['mango', 'papaya', 'pineapple'] cherry
遍歷 tuple 一般有兩種作法。
thistuple = ("apple", "banana", "cherry") for x in thistuple: print(x)
除了簡單粗暴的 for 循環,還能夠利用下標實現 for 操做,代碼以下:
thistuple = ("apple", "banana", "cherry") for i in range(len(thistuple)): print(thistuple[i])
也能夠經過 len()
獲取tuple 的長度,而後使用 while 循環,不過這裏記得在循環的過程當中,要記得將下標自增,以下代碼所示:
thistuple = ("apple", "banana", "cherry") i = 0 while i < len(thistuple): print(thistuple[i]) i = i + 1 PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py apple banana cherry
合併多個 tuple 也是很是簡單的,一般有二種作法。
能夠直接在兩個 tuple 中使用 + 操做,以下代碼所示:
tuple1 = ("a", "b" , "c") tuple2 = (1, 2, 3) tuple3 = tuple1 + tuple2 print(tuple3) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py ('a', 'b', 'c', 1, 2, 3)
若是想讓 tuple 中內容重複出現幾回,這個幾回可使用 x num
的操做,這裏的 num 就是幾回的意思,以下代碼所示:
fruits = ("apple", "banana", "cherry") mytuple = fruits * 2 print(mytuple) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py ('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
除了上面介紹的幾個,tuple 還有以下幾個內建方法。
譯文連接: https://www.w3schools.com/pyt...
更多高質量乾貨:參見個人 GitHub: python