元组(Tuple)是 Python 对象的集合,很像列表。存储在元组中的值序列可以是任何类型,并且它们由整数索引。
元组的值在语法上由“逗号”分隔。虽然这不是必需的,但更常见的是通过关闭括号中的值序列来定义元组。这有助于更轻松地理解 Python 元组。
在 Python 中,元组是通过放置由“逗号”分隔的值序列创建的,使用或不使用括号对数据序列进行分组。
注意:不使用括号创建 Python 元组称为元组打包。
# Creating an empty Tuple Tuple1 = () print("Initial empty Tuple: ") print(Tuple1) # Creating a Tuple # with the use of string Tuple1 = ('Geeks', 'For') print("\nTuple with the use of String: ") print(Tuple1) # Creating a Tuple with # the use of list list1 = [1, 2, 4, 5, 6] print("\nTuple using List: ") print(tuple(list1)) # Creating a Tuple # with the use of built-in function Tuple1 = tuple('Geeks') print("\nTuple with the use of function: ") print(Tuple1)
输出:
Initial empty Tuple: () Tuple with the use of String: ('Geeks', 'For') Tuple using List: (1, 2, 4, 5, 6) Tuple with the use of function: ('G', 'e', 'e', 'k', 's')
元组可以包含任意数量的元素和任意数据类型(如字符串、整数、列表等)。也可以用单个元素创建元组,但这有点棘手。括号中只有一个元素是不够的,必须有一个尾随的“逗号”才能使其成为一个元组。
# Creating a Tuple # with Mixed Datatype Tuple1 = (5, 'Welcome', 7, 'Geeks') print("\nTuple with Mixed Datatypes: ") print(Tuple1) # Creating a Tuple # with nested tuples Tuple1 = (0, 1, 2, 3) Tuple2 = ('python', 'geek') Tuple3 = (Tuple1, Tuple2) print("\nTuple with nested tuples: ") print(Tuple3) # Creating a Tuple # with repetition Tuple1 = ('Geeks',) * 3 print("\nTuple with repetition: ") print(Tuple1) # Creating a Tuple # with the use of loop Tuple1 = ('Geeks') n = 5 print("\nTuple with a loop") for i in range(int(n)): Tuple1 = (Tuple1,) print(Tuple1)
Tuple with Mixed Datatypes: (5, 'Welcome', 7, 'Geeks') Tuple with nested tuples: ((0, 1, 2, 3), ('python', 'geek')) Tuple with repetition: ('Geeks', 'Geeks', 'Geeks') Tuple with a loop ('Geeks',) (('Geeks',),) ((('Geeks',),),) (((('Geeks',),),),) ((((('Geeks',),),),),)
时间复杂度: O(1)
辅助空间: O(n)
元组是不可变的,通常,它们包含一系列异构元素,这些元素可以通过解包或索引访问(或者甚至在命名元组的情况下通过属性访问)。列表是可变的,它们的元素通常是同类的,可以通过遍历列表来访问。
注意:在元组的解包中,左侧变量的数量应该等于给定元组 a 中的值的数量。
# Accessing Tuple # with Indexing Tuple1 = tuple("Geeks") print("\nFirst element of Tuple: ") print(Tuple1[0]) # Tuple unpacking Tuple1 = ("Geeks", "For", "Geeks") # This line unpack # values of Tuple1 a, b, c = Tuple1 print("\nValues after unpacking: ") print(a) print(b) print(c)
First element of Tuple: G Values after unpacking: Geeks For Geeks
空间复杂度: O(1)
元组的连接是连接两个或多个元组的过程。连接是通过使用“+”运算符完成的。元组的连接总是从原始元组的末尾开始。其他算术运算不适用于元组。
注意 -只有相同的数据类型才能通过串联组合,如果将列表和元组组合在一起,则会出现错误。
# Concatenation of tuples Tuple1 = (0, 1, 2, 3) Tuple2 = ('Geeks', 'For', 'Geeks') Tuple3 = Tuple1 + Tuple2 # Printing first Tuple print("Tuple 1: ") print(Tuple1) # Printing Second Tuple print("\nTuple2: ") print(Tuple2) # Printing Final Tuple print("\nTuples after Concatenation: ") print(Tuple3)
Tuple 1: (0, 1, 2, 3) Tuple2: ('Geeks', 'For', 'Geeks') Tuples after Concatenation: (0, 1, 2, 3, 'Geeks', 'For', 'Geeks')
辅助空间: O(1)
对元组进行切片是为了从元组中获取特定范围或子元素切片。也可以对列表和数组进行切片。列表中的索引导致获取单个元素,而切片允许获取一组元素。
注意-负增量值也可用于反转元组的顺序。
# Slicing of a Tuple # Slicing of a Tuple # with Numbers Tuple1 = tuple('GEEKSFORGEEKS') # Removing First element print("Removal of First Element: ") print(Tuple1[1:]) # Reversing the Tuple print("\nTuple after sequence of Element is reversed: ") print(Tuple1[::-1]) # Printing elements of a Range print("\nPrinting elements between Range 4-9: ") print(Tuple1[4:9])
Removal of First Element: ('E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S') Tuple after sequence of Element is reversed: ('S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G') Printing elements between Range 4-9: ('S', 'F', 'O', 'R', 'G')
元组是不可变的,因此它们不允许删除其中的一部分。使用 del() 方法删除整个元组。
注意 -删除后打印元组会导致错误。
# Deleting a Tuple Tuple1 = (0, 1, 2, 3, 4) del Tuple1 print(Tuple1)
回溯(最近一次调用最后一次): 文件“/home/efa50fd0709dec08434191f32275928a.py”,第 7 行, 打印(元组 1) 中的 名称错误:未定义名称“元组 1”
原文链接:codingdict.net