Python干貨:了解元組與列表的使用和區別


元組是 Python 對象的集合,跟列表十分相似。下面進行簡單的對比。

列表與元組

1、python中的列表list是變量,而元組tuple是常量。

列表:是使用方括號[],元組:則是使用圓括號()

2、兩者都可以使用索引讀取值

列表

1.列表中的append()和extend()

都是對列表增加元素的方法,都不支持多參數

但是append()向列表中添加一個作為整體的對象,

extend()是把一個可迭代對象的內容迭代添加到列表中

2. 列表中的remove()、pop()和del

remove:刪除單個元素,刪除首個符合條件的元素,按值刪除,返回值為空

pop:刪除索引位置元素,無參情況下刪除最后一個元素,返回刪除的元素值

del:簡單粗暴,可傳索引值參數刪除符合條件的元素,也可不接參數整個刪除

元組

存儲在元組中的值序列可以是任何類型的,並且它們由整數編制索引。

元組的值在語法上用"逗號"分隔。

但通過關閉括號中的值序列來定義元組更為常見。

歡迎各位熱愛Python的小伙伴進群交流:610380249

 

學習Python的過程中遇到什么問題都可以一起交流。

創建一個空元組與創建帶一個元素的元組

在 Python 中,通過放置用"逗號"分隔的值序列(帶或不使用括號來分組數據序列)來創建元組。

注 :創建不使用括號的 Python 元組稱為元組打包。

tup1 = () # 創建空元組

tup2 = (1, )

#元組中只包含一個元素時,需要在元素后面添加逗號


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語句來刪除整個元組,如下實例:

tup = ('physics', 'chemistry', 1997, 2000)

print(tup)

del tup

print("After deleting tup:")

print(tup)


元組使用小括號,列表使用方括號。

元組創建很簡單,只需要在括號中添加元素,並使用逗號隔開即可。

tup1 = ('physics', 'chemistry', 1997, 2000)tup2 = (1, 2, 3, 4, 5 )tup3 = "a", "b", "c", "d"


元組與字符串類似,下標索引從0開始,可以進行截取,組合等。

修改元組

元組中的元素值是不允許修改的,但我們可以對元組進行連接組合,如下實例:

tup1 = (1,2,3,4)

tup2 = ('abc', 'xyz')

# 以下修改元組元素操作是非法的。# tup1[0] = 100

# 創建一個新的元組

tup3 = tup1 + tup2

print (tup3)


元組運算符

與字符串一樣,元組之間可以使用 + 號和 * 號進行運算。這就意味着他們可以組合和復制,運算后會生成一個新的元組。

元組索引,截取(切片)

因為元組也是一個序列,所以我們可以訪問元組中的指定位置的元素,也可以截取索引中的一段元素,如下所示:

L =('spam','Spam','SPAM!')


無關閉分隔符

任意無符號的對象,以逗號隔開,默認為元組,如下實例:

print ('abc', -4.24e93, 18+6.6j, 'xyz')

x, y = 1, 2

print ("Value of x , y : ", x,y)


創建具有混合數據類型的元組

元組可以包含任何數量的元素和任何數據類型(如字符串、整數、列表等)。

也可以使用單個元素創建元組。

括號中包含一個元素是不夠的,必須有一個尾隨的"逗號"才能使其成為元組。

#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',),),),),)


訪問元組

元組是不可變的,通常,它們包含一系列異構元素。

這些元素是通過解包或索引(甚至按屬性在命名元組的情況下訪問)。

列表是可變的,並且其元素通常是同質的,並且通過遍該列表進行遍時訪問。

注意:左側元組數的變量時,應等於給定元組 a 中的值數。

#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


圖組串聯

元組串聯是兩個或更多元組連接的過程。其他算術運算不適用於元對元。

串聯通過使用"+"運算符完成。元組串聯始終從原始元組末尾完成。

注意 -只有相同的數據類型可以與串聯結合,如果將列表和元組組合在一起,則會出現錯誤。

# 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')


圖們的切片

執行元組切片以從元組獲取特定范圍或子元素切片。

也可以對列表和數組進行切片。在列表中索引結果獲取單個元素,而且切片允許獲取一組元素。

注意- 負增量值也可用於反轉元數序列


# 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() 方法將刪除整個元組。

注意 -刪除后打印元組結果為錯誤。

# Deleting a Tuple

Tuple1 = (0, 1, 2, 3, 4)
del Tuple1

print(Tuple1)


內置方法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM