Python中的複合資料型別和資料結構是什麼?

PHPz
發布: 2023-08-19 16:45:15
轉載
1288 人瀏覽過

Python中的複合資料型別和資料結構是什麼?

在本文中,我們將解釋Python中的複合資料型別和資料結構。

到目前為止,變數只能儲存一個值。如果我們希望保存許多相關的值呢?

我們可以簡單地為每個變數建立不同的變數。

但是如果我們不知道會有多少個值呢?

如果我們希望在循環中使用這些值,該怎麼辦?

複合資料結構是可以儲存大量值的資料型別。

在Python中,有各種類型的複合資料結構。

  • 我們將主要集中在清單

  • 最後,我們將快速了解Sets, Tuples, and Dictionaries

列表

在Python中,列表是一個有序的序列,可以容納多種物件類型,如整數、字元或浮點數。在其他程式語言中,列表相當於數組。

列表只是由用逗號分隔並用方括號[]括起來的值組成的列表。

inputList = [“hello”, “tutorialspoint”, 1, 3.5, “python”]
登入後複製

列表操作

有許多操作可以對清單進行,以便從中建立表達式。

1)使用len()函數取得列表的大小

#使用len()函數取得列表的長度/大小(len()方法傳回物件中的項目數。當物件是列表時,len()函數傳回清單中的項目數),並創建一個變數來儲存它。

範例

# input list
lst = ["Hello", "TutorialsPoint", 78, "Hi", "Everyone"]

# getting list length
listLength = len(lst)

# Printing the size of a list
print("Size of a List = ", listLength)
登入後複製

輸出

('Size of a List = ', 5)
登入後複製

使用索引存取清單元素

術語 "indexing" 指的是根據元素在可迭代物件中的位置來取得元素。

索引從0開始。序列中的第一個元素以索引0表示。

負索引從-1開始。序列中的最後一個元素由索引-1表示。

範例

# input list
inputList =[1, 4, 8, 6, 2]

# accessing the list element at index 2 using positive indexing
print("Element at index 2:", inputList[2])

# accessing the last element in list using negative indexing
print("last element of an input list:", inputList[-1])
登入後複製

輸出

('Element at index 2:', 8)
('last element of an input list:', 2)
登入後複製

注意

當我們嘗試使用不存在或過大的索引時,會拋出一個 IndexError

迭代列表

使用For迴圈

以下程式使用for迴圈列印所有清單元素:

# input list
inputList = [10, 20, 30, 40, 50]

print("Input list elements:")
# traversing through all elements of the list using for loop
for element in inputList:
   # printing each element of the list
   print(element)
登入後複製

輸出

Input list elements:
10
20
30
40
50
登入後複製

清單項目上的重複運算子(*)

Python List還包括*運算符,它允許你建立一個新的列表,其中的元素重複指定的次數。

範例

以下程式使用*運算子重複給定次數的清單-

# input list
inputList = [5, 6, 7]

# Repeating the input list 2 times using the * operator
print(inputList * 2)
登入後複製

輸出

[5, 6, 7, 5, 6, 7]
登入後複製

在這裡,我們使用*運算子將隨機值列表乘以兩次,這樣輸出就是給定列表重複兩次。

Python中的元組

元組是一個不可變的序列資料類型,可以包含不同資料類型的元素。元組只是由逗號分隔的Python物件的集合。由於元組是靜態的,所以它們比列表更快。

列表和元組的語法有些不同。列表以方括號 [] 表示,而元組則以括號 () 表示。

元組切片

我們可以使用元組切片。它與我們使用字串和列表的方式類似。元組切片用於取得各種項。我們也使用切片運算子來執行元組切片。切片運算子可以用以下語法表示

[start:stop:step]
登入後複製

Example

的中文翻譯為:

範例

# Input tuple
givenTuple = ("Welcome", "this", "is", "TutorialsPoint", "Website", 10)

# Slicing with start and stop values(indices)
print('Tuple slicing from index 1 to index 6 :', givenTuple[1:6])

# Slicing with only stop values(indices)
print("Tuple slicing till index 7: ", givenTuple[:7])
登入後複製

輸出

Tuple slicing from index 1 to index 6 : ('this', 'is', 'TutorialsPoint', 'Website', 10)
Tuple slicing till index 7: ('Welcome', 'this', 'is', 'TutorialsPoint', 'Website', 10)
登入後複製

使用索引存取元組元素

與清單一樣,元組也使用索引來存取其元素。唯一的區別是元組是不可變的(不能被改變),而列表是可變的。

範例

的中文翻譯為:

範例

#
# input tuple
inputTuple = (1, 4, 8, 6, 2)

# accessing the tuple element at index 2 using positive indexing
print("Element at index 2:", inputTuple[2])

# accessing the last element in tuple using negative indexing
print("last element of an input tuple:", inputTuple[-1])
登入後複製

輸出

('Element at index 2:', 8)
('last element of an input tuple:', 2)
登入後複製

注意

當我們嘗試使用不存在或過大的索引時,會拋出一個 IndexError

Python中的字典

使用dict.keys()方法從字典中取得所有鍵的列表

使用keys() 函數將其應用於輸入的字典,然後使用list() 函數(將序列/可迭代物件轉換為列表)將結果轉換為列表,以列印字典的所有鍵。

Example

的中文翻譯為:

範例

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# Printing the list of keys of a dictionary using the keys() function

# list() methods convert an iterable into a list
print(list(demoDictionary.keys()))
登入後複製

輸出

[10, 12, 14]
登入後複製

結論

在這篇文章中,我們學習了複合資料型別和資料結構,以及它們的一些例子。

以上是Python中的複合資料型別和資料結構是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!