元組是 Python 對(duì)象的集合,跟列表十分相似。下面進(jìn)行簡(jiǎn)單的對(duì)比。
列表與元組
1、python中的列表list是變量,而元組tuple是常量。
列表:是使用方括號(hào)[],元組:則是使用圓括號(hào)()
2、兩者都可以使用索引讀取值
列表
1.列表中的append()和extend()
都是對(duì)列表增加元素的方法,都不支持多參數(shù)
但是append()向列表中添加一個(gè)作為整體的對(duì)象,
extend()是把一個(gè)可迭代對(duì)象的內(nèi)容迭代添加到列表中
2. 列表中的remove()、pop()和del
remove:刪除單個(gè)元素,刪除首個(gè)符合條件的元素,按值刪除,返回值為空
pop:刪除索引位置元素,無(wú)參情況下刪除最后一個(gè)元素,返回刪除的元素值
del:簡(jiǎn)單粗暴,可傳索引值參數(shù)刪除符合條件的元素,也可不接參數(shù)整個(gè)刪除
元組
存儲(chǔ)在元組中的值序列可以是任何類型的,并且它們由整數(shù)編制索引。
元組的值在語(yǔ)法上用"逗號(hào)"分隔。
但通過(guò)關(guān)閉括號(hào)中的值序列來(lái)定義元組更為常見(jiàn)。
創(chuàng)建一個(gè)空元組與創(chuàng)建帶一個(gè)元素的元組
在 Python 中,通過(guò)放置用"逗號(hào)"分隔的值序列(帶或不使用括號(hào)來(lái)分組數(shù)據(jù)序列)來(lái)創(chuàng)建元組。
注 :創(chuàng)建不使用括號(hào)的 Python 元組稱為元組打包。
tup1 = () # 創(chuàng)建空元組
tup2 = (1, )
#元組中只包含一個(gè)元素時(shí),需要在元素后面添加逗號(hào)
Python 程序演示在元組中添加的元素
# 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(geeen)
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, e, n)
Python的元組與列表類似,不同之處在于元組的元素不能修改。
刪除元組
元組中的元素值是不允許刪除的,但我們可以使用del語(yǔ)句來(lái)刪除整個(gè)元組,如下實(shí)例:
tup = (physics, chemistry, 1997, 2000)
print(tup)
del tup
print("After deleting tup:")
print(tup)
元組使用小括號(hào),列表使用方括號(hào)。
元組創(chuàng)建很簡(jiǎn)單,只需要在括號(hào)中添加元素,并使用逗號(hào)隔開(kāi)即可。
tup1 = (physics, chemistry, 1997, 2000)tup2 = (1, 2, 3, 4, 5 )tup3 = "a", "b", "c", "d"
元組與字符串類似,下標(biāo)索引從0開(kāi)始,可以進(jìn)行截取,組合等。
修改元組
元組中的元素值是不允許修改的,但我們可以對(duì)元組進(jìn)行連接組合,如下實(shí)例:
tup1 = (1,2,3,4)
tup2 = (abc, xyz)
# 以下修改元組元素操作是非法的。# tup1[0] = 100
# 創(chuàng)建一個(gè)新的元組
tup3 = tup1 + tup2
print (tup3)
元組運(yùn)算符
與字符串一樣,元組之間可以使用 + 號(hào)和 * 號(hào)進(jìn)行運(yùn)算。這就意味著他們可以組合和復(fù)制,運(yùn)算后會(huì)生成一個(gè)新的元組。
元組索引,截取(切片)
因?yàn)樵M也是一個(gè)序列,所以我們可以訪問(wèn)元組中的指定位置的元素,也可以截取索引中的一段元素,如下所示:
L =(spam,Spam,SPAM!)
無(wú)關(guān)閉分隔符
任意無(wú)符號(hào)的對(duì)象,以逗號(hào)隔開(kāi),默認(rèn)為元組,如下實(shí)例:
print (abc, -4.24e93, 18+6.6j, xyz)
x, y = 1, 2
print ("Value of x , y : ", x,y)
創(chuàng)建具有混合數(shù)據(jù)類型的元組
元組可以包含任何數(shù)量的元素和任何數(shù)據(jù)類型(如字符串、整數(shù)、列表等)。
也可以使用單個(gè)元素創(chuàng)建元組。
括號(hào)中包含一個(gè)元素是不夠的,必須有一個(gè)尾隨的"逗號(hào)"才能使其成為元組。
#Creating a Tuple
#with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = (python, geeen)
Tuple3 = (Tuple1, Tuple2)
print("/nTuple with nested tuples: ")
print(Tuple3)
#Creating a Tuple
#with repetition
Tuple1 = (Geeen,) * 3
print("/nTuple with repetition: ")
print(Tuple1)
#Creating a Tuple
#with the use of loop
Tuple1 = (Geeen)
n = 5
print("/nTuple with a loop")
for i in range(int(n)):
Tuple1 = (Tuple1,)
print(Tuple1)
輸出:
Tuple with Mixed Datatypes:
(5, Welcome, 7, Geeen)
Tuple with nested tuples:
((0, 1, 2, 3), (python, geeen))
Tuple with repetition:
(Geeen, Geeen, Geeen)
Tuple with a loop
(Geeen,)
((Geeen,),)
(((Geeen,),),)
((((Geeen,),),),)
(((((Geeen,),),),),)
學(xué)習(xí)更多python知識(shí)訪問(wèn)如下鏈接地址
??
訪問(wèn)元組
元組是不可變的,通常,它們包含一系列異構(gòu)元素。
這些元素是通過(guò)解包或索引(甚至按屬性在命名元組的情況下訪問(wèn))。
列表是可變的,并且其元素通常是同質(zhì)的,并且通過(guò)遍該列表進(jìn)行遍時(shí)訪問(wèn)。
注意:左側(cè)元組數(shù)的變量時(shí),應(yīng)等于給定元組 a 中的值數(shù)。
#Accessing Tuple
#with Indexing
Tuple1 = tuple("Geeen")
print("/nFirst element of Tuple: ")
print(Tuple1[1])
#Tuple unpacking
Tuple1 = ("Geeen", "For", "Geeen")
#This line unpack
#values of Tuple1
a, b, c = Tuple1
print("/nValues after unpacking: ")
print(a)
print(b)
print(c)
輸出:
First element of Tuple:
e
Values after unpacking:
Geeen
For
Geeen
圖組串聯(lián)
元組串聯(lián)是兩個(gè)或更多元組連接的過(guò)程。其他算術(shù)運(yùn)算不適用于元對(duì)元。
串聯(lián)通過(guò)使用"+"運(yùn)算符完成。元組串聯(lián)始終從原始元組末尾完成。
注意 -只有相同的數(shù)據(jù)類型可以與串聯(lián)結(jié)合,如果將列表和元組組合在一起,則會(huì)出現(xiàn)錯(cuò)誤。
# Concatenaton of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = (Geeen, For, Geeen)
Tuple3 = Tuple1 + Tuple2
# Printing first Tuple
print("Tuple 1: ")
print(Tuple1)
# Printing Second Tuple
print("/nTuple2: ")
print(Tuple2)
# Printing Final Tuple
print("/nTuples after Concatenaton: ")
print(Tuple3)
輸出:
Tuple 1:
(0, 1, 2, 3)
Tuple2:
(Geeen, For, Geeen)
Tuples after Concatenaton:
(0, 1, 2, 3, Geeen, For, Geeen)
圖們的切片
執(zhí)行元組切片以從元組獲取特定范圍或子元素切片。
也可以對(duì)列表和數(shù)組進(jìn)行切片。在列表中索引結(jié)果獲取單個(gè)元素,而且切片允許獲取一組元素。
注意- 負(fù)增量值也可用于反轉(zhuǎn)元數(shù)序列
# Slicing of a Tuple
# wit
輸出:
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() 方法將刪除整個(gè)元組。
注意 -刪除后打印元組結(jié)果為錯(cuò)誤。
# Deleting a Tuple
Tuple1 = (0, 1, 2, 3, 4)
del Tuple1
print(Tuple1)
內(nèi)置方法
?學(xué)習(xí)更多python知識(shí)訪問(wèn)如下鏈接地址
??