Python - 元组
2023-09-17 22:16:01
元组是不同数据类型的元素的不可变(不可更改)集合。 它是一个有序集合,因此它保留了定义它们的元素的顺序。
元组的定义是将元素括在括号()
,用逗号分隔。
下面声明一个元组类型变量。
tpl=() # empty tuple
print(tpl) #output: ()
names = ('Jeff', 'Bill', 'Steve', 'Yash') # string tuple
print(names) #output:('Jeff', 'Bill', 'Steve', 'Yash')
nums = (1, 2, 3, 4, 5) # int tuple
print(nums) #output:(1, 2, 3, 4, 5)
employee=(1, 'Steve', True, 25, 12000) # heterogeneous data tuple
print(employee) #output:(1, 'Steve', True, 25, 12000)
但是,没有必要将元组元素括在括号中。元组对象可以包含由逗号分隔的元素,不带括号。
names = 'Jeff', 'Bill', 'Steve', 'Yash' # string tuple
print(names) #output: ('Jeff', 'Bill', 'Steve', 'Yash')
nums = 1, 2, 3, 4, 5 # int tuple
print(nums) #output: (1, 2, 3, 4, 5)
employee=1, 'Steve', True, 25, 12000 # heterogeneous data tuple
print(employee) #output: (1, 'Steve', True, 25, 12000)
元组不能用单个元素声明,除非后跟逗号。
names = ('Jeff') # considered as string type
print(names) #output: 'Jeff'
print(type(names)) #output: <class 'string'>
names = ('Jeff',) # tuple with single element
print(names) #output: (Jeff)
print(type(names)) #output: <class 'tuple'>
访问元组元素(Access Tuple Elements)
元组中的每个元素都可以通过方括号 [] 中的索引访问。索引从零开始,以(元素数 - 1)结束,如下所示。
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(names[0]) #output: 'Jeff'
print(names[1]) #output: 'Bill'
print(names[2]) #output: 'Steve'
print(names[3]) #output: 'Yash'
nums = (1, 2, 3, 4, 5)
print(nums[0]) #output: 1
print(nums[1]) #output: 2
print(nums[4]) #output: 5
元组也支持负索引,与列表类型相同。第一个元素的负索引从 -number of elements
开始,最后一个元素以 -1 结束。
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(names[-4]) #output: 'Jeff'
print(names[-3]) #output: 'Bill'
print(names[-2]) #output: 'Steve'
print(names[-1]) #output: 'Yash'
如果指定索引处的元素不存在,则将抛出错误"索引超出范围"。
s = names[5] #IndexError: tuple index out of range
元组元素可以解压缩并分配给变量,如下所示。但是,变量的数量必须与元组中的元素数量匹配;否则,将引发错误。
names = ('Jeff', 'Bill', 'Steve', 'Yash')
a, b, c, d = names # unpack tuple
print(a, b, c, d)
更新或删除元组元素(Update or Delete Tuple Elements)
元组是不可更改的。因此,创建元组后,不允许任何试图更改其内容的操作。
例如,尝试修改或删除元组names
元素将导致错误。
但是,您可以使用 del
关键字删除整个元组。
names = ('Jeff', 'Bill', 'Steve', 'Yash')
names[0] = 'Swati' #throws error
del names[0] #throws error
del names #delete names variable
元组类(Tuple Class)
元组的基础类型是元组类。 使用 type()
函数检查变量的类型。
names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(type(names)) #output: <class 'tuple'>
nums = (1,2,3,4,5)
print(type(nums)) #output: <class 'tuple'>
tuple()
构造函数用于将任何可迭代类型转换为元组类型。
tpl = tuple('Hello')
print(tpl) #output: ('H','e','l','l','o')
tpl = tuple([1,2,3,4,5])
print(tpl) #output: (1,2,3,4,5)
tpl = tuple({1,2,3,4,5}) # converts set to tuple
print(tpl) #output: (1,2,3,4,5)
tpl = tuple({1:"One",2:"Two"}) # converts dictionary to tuple
print(tpl) #output: (1,2)
元组操作(Tuple Operations)
与字符串一样,元组对象也是一个序列。因此,与字符串一起使用的运算符也可用于元组。
Operator | Example |
---|---|
The + operator returns a tuple containing all the elements of the first and the second tuple object. | t1=(1,2,3) t2=(4,5,6) print(t1+t2) #(1, 2, 3, 4, 5, 6) t2+(7,) #(4, 5, 6, 7)Try it |
The * operator Concatenates multiple copies of the same tuple. | t1=(1,2,3) t1*4 #(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)Try it |
The [] operator Returns the item at the given index. A negative index counts the position from the right side. | t1=(1,2,3,4,5,6) t1[3] # 4 t1[-2] #5Try it |
The [:] operator returns the items in the range specified by two index operands separated by the : symbol.
| t1=(1,2,3,4,5,6) t1[1:3] #(2, 3) t1[3:] #(4, 5, 6) t1[:3] #(1, 2, 3)Try it |
The in operator returns true if an item exists in the given tuple. | t1=(1,2,3,4,5,6) 5 in t1 #True 10 in t1 #FalseTry it |
The not in operator returns true if an item does not exist in the given tuple. | t1=(1,2,3,4,5,6) 4 not in t1 #False 10 not in t1 #TrueTry it |
本文内容总结: