Python基础篇 tuple

  • 2019 年 12 月 26 日
  • 筆記

tuple是有序异构容器,用于存储异构元素,列表一旦创建,其内容不可改变。

创建元组核查元素是否位于元组Append , Insert , Modify & delete 元组元素

创建元组

首先定义元组

1In [1]: # 直接创建元组,','分割,使用圆括号  2   ...: tupleObj = ('Riti', 31, 'London', 78.88)  3   ...: tupleObj  4Out[1]: ('Riti', 31, 'London', 78.88)

不使用圆括号

1In [2]: # 直接创建,','分割,不使用圆括号  2   ...: tupleObj = 'Jake', 23, 'paris' , 89.33  3   ...: tupleObj  4Out[2]: ('Jake', 23, 'paris', 89.33)

创建空元组

1In [3]: # 创建空tuple  2   ...: emptyTuple = ()  3   ...: emptyTuple  4Out[3]: ()

通过列表创建元组

1In [4]: # 通过 list 创建 tuple  2   ...: listOfNumbers = [12 , 34, 45, 22, 33 ]  3   ...: tupleObj = tuple(listOfNumbers)  4   ...: tupleObj  5Out[4]: (12, 34, 45, 22, 33)

使用 for 循环遍历元素

 1In [5]: # 使用 for 循环遍历元素   2   ...: tupleObj = ('Riti', 31, 'London', 78.88)   3   ...:   4   ...: for elem in tupleObj:   5   ...:     print(elem)   6   ...:   7Riti   831   9London  1078.88

核查元素是否位于元组

初始化元组

1In [6]: # 初始化tuple  2   ...: tupleObj = (12 , 34, 45, 22, 33 , 67, 34, 56 )  3   ...: tupleObj  4Out[6]: (12, 34, 45, 22, 33, 67, 34, 56)

使用成员运算符 in / not in 判断元素是否位于 tuple

 1In [7]: # 使用成员运算符 in / not in 判断元素是否位于 tuple   2   ...: if 34 in tupleObj:   3   ...:     print("Element Found in Tuple")   4   ...: else:   5   ...:     print("Element not Found in Tuple")   6   ...: print('*'*30)   7   ...: if 1001 not in tupleObj:   8   ...:     print("Yes, element Not In tuple")   9   ...: else:  10   ...:     print("Element is in Tuple")  11   ...:  12Element Found in Tuple  13******************************  14Yes, element Not In tuple

查找元素的索引,语法:tuple.index(x)

 1In [8]: # 查找元素的索引,语法:tuple.index(x)   2   ...: try :   3   ...:     pos = tupleObj.index(34)   4   ...:     print("Element 34 Found at : " , pos)   5   ...: except ValueError as e:   6   ...:     print(e)   7   ...: print('*'*30)   8   ...: try :   9   ...:     pos = tupleObj.index(24)  10   ...:     print("Element 24 Found at : " , pos)  11   ...: except ValueError as e:  12   ...:     print(e)  13   ...:  14Element 34 Found at :  1  15******************************  16tuple.index(x): x not in tuple

核实元素在 tuple 中出现次数,语法:tuple.count(elem)

1In [9]: # 核实元素在 tuple 中出现次数,语法:tuple.count(elem)  2   ...: count = tupleObj.count(34)  3   ...:  4   ...: print("Count of 34 in tuple is : ", count)  5Count of 34 in tuple is :  2

使用 count() 判断元素是否位于 tuple

1In [10]: # 使用 count() 判断元素是否位于 tuple  2    ...: if tupleObj.count(34) > 0 :  3    ...:     print("34 Found in Tuple")  4    ...: else:  5    ...:     print("34 Not Found in Tuple")  6    ...:  734 Found in Tuple

Append , Insert , Modify & delete 元组元素

元组是不可变的,但是有时候我们需要修改元组,此时只能创建新元组来实现 使用运算符 + 并创建新元组

1In [11]: # 使用运算符 + 并创建新元组  2    ...: tupleObj = (12 , 34, 45, 22, 33 )  3    ...: print("Original Tuple : ", tupleObj)  4    ...: tupleObj = tupleObj + (19 ,)  5    ...: print("Modified Tuple : ", tupleObj)  6Original Tuple :  (12, 34, 45, 22, 33)  7Modified Tuple :  (12, 34, 45, 22, 33, 19)

使用切片在指定索引插入元素

1In [12]: # 使用切片在指定索引插入元素  2    ...: tupleObj = (12 , 34, 45, 22, 33 )  3    ...: print("Original Tuple : ", tupleObj)  4    ...: n = 2  5    ...: # Insert 19 in tuple at index 2  6    ...: tupleObj = tupleObj[ : n ] + (19 ,) + tupleObj[n : ]  7    ...: print("Modified Tuple : ", tupleObj)  8Original Tuple :  (12, 34, 45, 22, 33)  9Modified Tuple :  (12, 34, 19, 45, 22, 33)

使用切片修改/置换指定索引的元素

1In [13]: # 使用切片修改/置换指定索引的元素  2    ...: tupleObj =  (12, 34, 19, 45, 22, 33, 19)  3    ...: print("Original Tuple : ", tupleObj)  4    ...: n = 2  5    ...: # Replace the element at index 2 to 'Test'  6    ...: tupleObj = tupleObj[ : n] + ('test' ,) + tupleObj[n + 1 : ]  7    ...: print("Modified Tuple : ", tupleObj)  8Original Tuple :  (12, 34, 19, 45, 22, 33, 19)  9Modified Tuple :  (12, 34, 'test', 45, 22, 33, 19)

使用切片删除指定索引的元素

1In [14]: # 使用切片删除指定索引的元素  2    ...: tupleObj =(12, 34, 'test', 45, 22, 33, 19)  3    ...: print("Original Tuple : ", tupleObj)  4    ...: n = 2  5    ...: # Delete the element at index 2  6    ...: tupleObj = tupleObj[ : n ] + tupleObj[n+1 : ]  7    ...: print("Modified Tuple : ", tupleObj)  8Original Tuple :  (12, 34, 'test', 45, 22, 33, 19)  9Modified Tuple :  (12, 34, 45, 22, 33, 19)