拜访 tuple
能够应用 index 的形式对 tuple 进行拜访,比方上面拜访 tuple 中的第二个元素。
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.pybanana
正数 index
正数索引意味着从后往前计算,比如说: -1 示意最初一项, -2 示意倒数第二项,代码如下:
thistuple = ("apple", "banana", "cherry")print(thistuple[-1])
范畴 index
能够指定一个范畴的 start 和 end 来指定一个 tuple 的 子区间,返回值就是一个新生成的 tuple,如下代码所示:
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,如下代码所示:
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')
查看 item 是否存在
查看 tuple 中的某一项是否存在,能够应用 in 关键词。
thistuple = ("apple", "banana", "cherry")if "apple" in thistuple: print("Yes, 'apple' is in the fruits tuple")
更新 tuple
tuple 是不可批改的,意味着一旦 tuple 创立好之后,你不能对其进行新增,删除,批改,但也有一些变通方法。
批改 tuple 值
变通方法就是,能够先将 tuple 转成 list,而后在 list 上进行批改,最初再将 list 转成 tuple 即可,如下代码所示:
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')
tuple中新增 / 删除
同样的情理,还是应用 list 作为两头转换,实现 tuple 的 add / remove 操作,代码如下:
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,那如何将 tuple 中的值肢解到几个变量中呢? 这就须要应用 unpacking 操作,代码如下:
fruits = ("apple", "banana", "cherry")(green, yellow, red) = fruitsprint(green)print(yellow)print(red)PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.pyapplebananacherry
tuple 中的 * 操作
如果须要肢解的 tuple 个数大于右边的变量个数,那么能够将一个变量申明成 *
,示意将 多余的 tuple 元素作为一个汇合赋给 *
号变量,如果不明确的话,参考上面代码:
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")(green, yellow, *red) = fruitsprint(green)print(yellow)print(red)PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.pyapplebanana['cherry', 'strawberry', 'raspberry']
同样这个 *号变量
也能够指定任意地位,如下代码所示:
fruits = ("apple", "mango", "papaya", "pineapple", "cherry")(green, *tropic, red) = fruitsprint(green)print(tropic)print(red)PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.pyapple['mango', 'papaya', 'pineapple']cherry
tuple 遍历
遍历 tuple 通常有两种做法。
- 应用 for 循环
thistuple = ("apple", "banana", "cherry")for x in thistuple: print(x)
- 应用 range() + len()
除了简略粗犷的 for 循环,还能够利用下标实现 for 操作,代码如下:
thistuple = ("apple", "banana", "cherry")for i in range(len(thistuple)): print(thistuple[i])
- 应用 while 循环
也能够通过 len()
获取tuple 的长度,而后应用 while 循环,不过这里记得在循环的过程中,要记得将下标自增,如下代码所示:
thistuple = ("apple", "banana", "cherry")i = 0while i < len(thistuple): print(thistuple[i]) i = i + 1PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.pyapplebananacherry
tuple 合并
合并多个 tuple 也是非常简单的,通常有二种做法。
- 操作
能够间接在两个 tuple 中应用 + 操作,如下代码所示:
tuple1 = ("a", "b" , "c")tuple2 = (1, 2, 3)tuple3 = tuple1 + tuple2print(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)
- x 操作
如果想让 tuple 中内容反复呈现几次,这个几次能够应用 x num
的操作,这里的 num 就是几次的意思,如下代码所示:
fruits = ("apple", "banana", "cherry")mytuple = fruits * 2print(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 办法
除了下面介绍的几个,tuple 还有如下几个内建办法。
译文链接:https://www.w3schools.com/pyt...
更多高质量干货:参见我的 GitHub: python