Python中的複合資料型別和資料結構是什麼?
在本文中,我們將解釋Python中的複合資料型別和資料結構。
到目前為止,變數只儲存一個值。如果我們想儲存許多相關的值怎麼辦?
我們可以為每個值建立不同的變數。
但是如果我們不知道有多少值呢?
如果我們想在迴圈中使用這些值怎麼辦?
複合資料結構是可以儲存大量值的 資料型別。
列表
在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)
使用索引訪問列表元素
術語“索引”指的是基於其在可迭代物件中的位置的可迭代物件的元素。
索引從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列表還包括*運算子,它允許你建立一個新的列表,其中元素重複指定次數。
示例
下面的程式使用*運算子將列表重複給定的次數:
# 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]
示例
# 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()函式(將序列/可迭代物件轉換為列表)將結果轉換為列表。
示例
# 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]
結論
在本文中,我們學習了複合資料型別和資料結構,以及一些示例。