Python 元组
Python 的元组与列表类似,不同之处在于元组的元素不能修改:unchangeable
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
如下实例:
>>> tup1 = ('Google', 'Nasus', 1997, 2000);
>>> tup2 = (1, 2, 3, 4, 5 );
>>> tup3 = "a", "b", "c", "d"; # 不需要括号也可以
>>> type(tup3)
<class 'tuple'>
创建空元组
tup1 = ();
元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用:
>>> tup1 = (50)
>>> type(tup1) # 不加逗号,类型为整型
<class 'int'>
>>> tup1 = (50,)
>>> type(tup1) # 加上逗号,类型为元组
<class 'tuple'>
元组与字符串类似,下标索引从0开始,可以进行截取,组合等。
创建非空元组
It is also possible to use the tuple() constructor to make a tuple *tuple()生成器
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)
结果:
('apple', 'banana', 'cherry')
访问元组
access tuple terms
元组可以使用下标索引来访问元组中的值,如下实例:
tup1 = ('Google', 'Xiaomi', 2011, 0120)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
以上实例输出结果:
tup1[0]: Google
tup2[1:5]: (2, 3, 4, 5)
indexing 索引
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
结果:
cherry
range of index 索引范围
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new tuple with the specified items.
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5]
结果:
('cherry', 'orange', 'kiwi')
修改元组
元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下实例:
tup1 = (12, 34.56);
tup2 = ('nasus', 'czy')
以下修改元组元素操作是非法的。
tup1[0] = 100
创建一个新的元组
tup3 = tup1 + tup2;
print (tup3)
以上实例输出结果:
(12, 34.56, 'nasus', 'czy')
通过转换成列表来修改元组 但删除和添加不可行
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
结果:
("apple", "kiwi", "cherry")
删除元组
元组中的元素值是不允许删除的,但我们可以使用 del 语句来删除整个元组,如下实例:
tup = ('Google', 'Runoob', 1997, 2000)
print (tup)
del tup;
print ("删除后的元组 tup : ")
print (tup)
以上实例元组被删除后,输出变量会有异常信息,输出如下所示:
删除后的元组 tup :
Traceback (most recent call last):
File "test.py", line 8, in <module>
print (tup)
NameError: name 'tup' is not defined
遍历元组
You can loop through the tuple items by using a for loop.(for 循环)
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
结果:
apple
banana
cherry
Tuple Methods
Python has two built-in methods that you can use on tuples.
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was found
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5)
print(x)
<<<< 2
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.index(8)
print(x)
<<<< 3
The index() method finds the first occurrence of the specified value.
The index() method raises an exception if the value is not found.
元组运算符
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。如下所示:
运行实例如下:
>>> L = ('Tengcent', 'Taobao', 'Xiaomi')
>>> L[2]
'Xiaomi'
>>> L[-2]
'Taobao'
>>> L[1:]
('Taobao', 'Xiaomi')
元组内置函数
Python 元组包含了以下内置函数