📜  Python Tuple

📅  最后修改于: 2020-10-24 09:09:10             🧑  作者: Mango

Python Tuple(元组)

Python Tuple用于存储不可变的Python对象的序列。元组与列表相似,因为可以更改存储在列表中的项目的值,而元组是不可变的,并且不能更改存储在元组中的项目的值。

创建一个元组

元组可以写成用小括号()括起来的逗号分隔(,)值的集合。括号是可选的,但是使用是一种很好的做法。元组可以定义如下。

T1 = (101, "Peter", 22)  
T2 = ("Apple", "Banana", "Orange")   
T3 = 10,20,30,40,50

print(type(T1))
print(type(T2))
print(type(T3))

输出:




注意:不使用括号创建的元组也称为元组包装。

可以如下创建一个空的元组。

用单个元素创建元组稍有不同。我们需要在元素后面加上逗号以声明元组。

tup1 = ("JavaTpoint")
print(type(tup1))
#Creating a tuple with single element 
tup2 = ("JavaTpoint",)
print(type(tup2))

输出:



元组的索引方式与列表相同。元组中的项目可以通过使用其特定的索引值进行访问。

考虑下面的元组示例:

示例-1

tuple1 = (10, 20, 30, 40, 50, 60)  
print(tuple1)  
count = 0  
for i in tuple1:  
    print("tuple1[%d] = %d"%(count, i)) 
    count = count+1

输出:

(10, 20, 30, 40, 50, 60)
tuple1[0] = 10
tuple1[1] = 20
tuple1[2] = 30
tuple1[3] = 40
tuple1[4] = 50
tuple1[5] = 60

示例-2

tuple1 = tuple(input("Enter the tuple elements ..."))
print(tuple1)  
count = 0  
for i in tuple1:  
    print("tuple1[%d] = %s"%(count, i)) 
    count = count+1

输出:

Enter the tuple elements ...123456
('1', '2', '3', '4', '5', '6')
tuple1[0] = 1
tuple1[1] = 2
tuple1[2] = 3
tuple1[3] = 4
tuple1[4] = 5
tuple1[5] = 6

元组的索引方式与列表相同。元组中的项目可以通过使用其特定的索引值进行访问。

在本教程的这一部分中,我们将看到元组的所有这些方面。

元组索引和切片

元组中的索引和切片类似于列表。元组中的索引从0开始,到length(tuple)-1。

可以使用index []运算符访问元组中的项目。 Python还允许我们使用冒号运算符访问元组中的多个项目。

考虑下图以了解详细的索引编制和切片。

考虑以下示例:

tup = (1,2,3,4,5,6,7)
print(tup[0])
print(tup[1])
print(tup[2])
# It will give the IndexError
print(tup[8])

输出:

1
2
3
tuple index out of range

在上面的代码中,元组有7个元素,表示0到6。我们试图访问在元组之外引发了IndexError的元素。

tuple = (1,2,3,4,5,6,7)
#element 1 to end
print(tuple[1:])
#element 0 to 3 element 
print(tuple[:4])
#element 1 to 4 element
print(tuple[1:5]) 
# element 0 to 6 and take step of 2
print(tuple[0:6:2])

输出:

(2, 3, 4, 5, 6, 7)
(1, 2, 3, 4)
(1, 2, 3, 4)
(1, 3, 5)

负索引

元组元素也可以通过使用负索引来访问。索引-1表示最右边的元素,索引-2表示倒数第二个元素,依此类推。

使用负索引从左到右遍历元素。考虑以下示例:

tuple1 = (1, 2, 3, 4, 5)  
print(tuple1[-1])  
print(tuple1[-4])  
print(tuple1[-3:-1])
print(tuple1[:-1])
print(tuple1[-2:])

输出:

5
2
(3, 4)
(1, 2, 3, 4)
(4, 5)

删除元组

与列表不同,由于元组是不可变的,因此不能使用del关键字删除元组项目。要删除整个元组,可以在元组名称中使用del关键字。

考虑以下示例。

tuple1 = (1, 2, 3, 4, 5, 6)  
print(tuple1)  
del tuple1[0]  
print(tuple1)  
del tuple1  
print(tuple1)  

输出:

(1, 2, 3, 4, 5, 6)
Traceback (most recent call last):
  File "tuple.py", line 4, in 
    print(tuple1)
NameError: name 'tuple1' is not defined

元组基本操作

串联(+),重复(*),成员资格(in)等运算运算符的工作方式与使用列表相同。请考虑下表以获取更多详细信息。

假设声明了元组t =(1、2、3、4、5)和元组t1 =(6、7、8、9)。

Operator Description Example
Repetition The repetition operator enables the tuple elements to be repeated multiple times.
T1*2 = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
Concatenation It concatenates the tuple mentioned on either side of the operator.
T1+T2 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
Membership It returns true if a particular item exists in the tuple otherwise false
print (2 in T1) prints True.
Iteration The for loop is used to iterate over the tuple elements.
for i in T1: 
    print(i)

Output

1
2
3
4
5
Length It is used to get the length of the tuple.
len(T1) = 5

Python Tuple内置函数

SN Function Description
1 cmp(tuple1, tuple2) It compares two tuples and returns true if tuple1 is greater than tuple2 otherwise false.
2 len(tuple) It calculates the length of the tuple.
3 max(tuple) It returns the maximum element of the tuple
4 min(tuple) It returns the minimum element of the tuple.
5 tuple(seq) It converts the specified sequence to the tuple.

在哪里使用元组?

在以下情况下,使用元组代替列表。

1.使用元组而不是列表可以使我们清楚地知道元组数据是恒定的,不能更改。

2.元组可以模拟不带键的字典。考虑以下嵌套结构,可以将其用作字典。

[(101, "John", 22), (102, "Mike", 28),  (103, "Dustin", 30)]

列表与元组

SN List Tuple
1 The literal syntax of list is shown by the []. The literal syntax of the tuple is shown by the ().
2 The List is mutable. The tuple is immutable.
3 The List has the a variable length. The tuple has the fixed length.
4 The list provides more functionality than a tuple. The tuple provides less functionality than the list.
5 The list is used in the scenario in which we need to store the simple collections with no constraints where the value of the items can be changed. The tuple is used in the cases where we need to store the read-only collections i.e., the value of the items cannot be changed. It can be used as the key inside the dictionary.
6 The lists are less memory efficient than a tuple. The tuples are more memory efficient because of its immutability.