Python 入门系列 —— 18. Tuple 的 CURD 操做

访问 tuple

可使用 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

负数 index

负数索引意味着从后往前计算,好比说: -1 表示最后一项, -2 表示倒数第二项,代码以下:git

thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])

范围 index

能够指定一个范围的 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')

检查 item 是否存在

检查 tuple 中的某一项是否存在,可使用 in 关键词。app

thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
  print("Yes, 'apple' is in the fruits tuple")

更新 tuple

tuple 是不可修改的,意味着一旦 tuple 建立好以后,你不能对其进行新增,删除,修改,但也有一些变通方法。ui

修改 tuple 值

变通方法就是,能够先将 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')

tuple中新增 / 删除

一样的道理,仍是使用 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,那如何将 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 个数大于左边的变量个数,那么能够将一个变量声明成 * ,表示将 多余的 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 遍历

遍历 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 = 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 也是很是简单的,一般有二种作法。

    • 操做

能够直接在两个 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)
  • x 操做

若是想让 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 方法

除了上面介绍的几个,tuple 还有以下几个内建方法。

译文连接: https://www.w3schools.com/pyt...

更多高质量干货:参见个人 GitHub: python

相关文章
相关标签/搜索