清單(Lists)就像用其他語言聲明的數組一樣。清單不一定總是同質的,這使其成為Python中最強大的工具。單個清單可能包含資料類型,例如整數,字元串以及對象。清單是可變的,是以即使在建立後也可以對其進行更改。
Python中的List是有序的,并且有一個确定的計數。清單中的元素按照一定的順序進行索引,并且清單的索引是在0為第一個索引的情況下完成的。清單中的每個元素在清單中都有其明确的位置,這允許複制清單中的元素,每個元素都有其獨特的位置和可信度。
注意:清單是用于保留資料序列并對其進行進一步疊代的有用工具。
建立清單
Python中的清單可以通過将序列放在方括号[]中來建立。與集合不同,list不需要内置函數來建立list。
注意:與集合不同,清單可能包含可變元素。
# Python program to demonstrate # Creation of List # Creating a List List = [] print("Intial blank List: ") print(List) # Creating a List with # the use of a String List = ['GeeksForGeeks'] print("List with the use of String: ") print(List) # Creating a List with # the use of multiple values List = ["Geeks", "For", "Geeks"] print("List containing multiple values: ") print(List[0]) print(List[2]) # Creating a Multi-Dimensional List # (By Nesting a list inside a List) List = [['Geeks', 'For'] , ['Geeks']] print("Multi-Dimensional List: ") print(List)
輸出:
Intial blank List: []List with the use of String: ['GeeksForGeeks']List containing multiple values: GeeksGeeksMulti-Dimensional List: [['Geeks', 'For'], ['Geeks']]
建立包含多個不同或重複元素的清單
清單可能包含具有不同位置的重複值,是以,在建立清單時,可以将多個不同或重複的值作為序列傳遞。
# Creating a List with # the use of Numbers # (Having duplicate values) List = [1, 2, 4, 4, 3, 3, 3, 6, 5] print("List with the use of Numbers: ") print(List) # Creating a List with # mixed type of values # (Having numbers and strings) List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks'] print("List with the use of Mixed Values: ") print(List)
輸出:
List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5]List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
向清單中添加元素
使用append()方法
可以使用内置的append()函數将元素添加到清單中。使用append()方法一次隻能向清單中添加一個元素,如果使用append()方法添加多個元素,則使用循環。元組也可以使用append方法添加到清單中,因為元組是不可變的。與集合不同,還可以使用append()方法将清單添加到現有清單中。
# Python program to demonstrate # Addition of elements in a List # Creating a List List = [] print("Initial blank List: ") print(List) # Addition of Elements # in the List List.append(1) List.append(2) List.append(4) print("List after Addition of Three elements: ") print(List) # Adding elements to the List # using Iterator for i in range(1, 4): List.append(i) print("List after Addition of elements from 1-3: ") print(List) # Adding Tuples to the List List.append((5, 6)) print("List after Addition of a Tuple: ") print(List) # Addition of List to a List List2 = ['For', 'Geeks'] List.append(List2) print("List after Addition of a List: ") print(List)
輸出:
Initial blank List: []List after Addition of Three elements: [1, 2, 4]List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3]List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, (5, 6)]List after Addition of a List: [1, 2, 4, 1, 2, 3, (5, 6), ['For', 'Geeks']]
使用insert()方法
append()方法隻适用于在清單末尾添加元素,對于在所需位置添加元素,則使用insert()方法。與隻接受一個參數的append()不同,insert()方法需要兩個參數(位置、值)。
# Python program to demonstrate # Addition of elements in a List # Creating a List List = [1,2,3,4] print("Initial List: ") print(List) # Addition of Element at # specific Position # (using Insert Method) List.insert(3, 12) List.insert(0, 'Geeks') print("List after performing Insert Operation: ") print(List)
輸出:
Initial List: [1, 2, 3, 4]List after performing Insert Operation: ['Geeks', 1, 2, 3, 12, 4]
使用extend()方法
除了append()和insert()方法之外,還有一個用于添加元素的方法extend(),該方法用于在清單末尾同時添加多個元素。
注意:append()和extend()方法隻能在末尾添加元素。
# Python program to demonstrate # Addition of elements in a List # Creating a List List = [1,2,3,4] print("Initial List: ") print(List) # Addition of multiple elements # to the List at the end # (using Extend Method) List.extend([8, 'Geeks', 'Always']) print("List after performing Extend Operation: ") print(List)
輸出:
Initial List: [1, 2, 3, 4]List after performing Extend Operation: [1, 2, 3, 4, 8, 'Geeks', 'Always']
通路清單中的元素
要通路清單項,請參考索引号。使用索引運算符[]通路清單中的項。索引必須是整數。使用嵌套索引通路嵌套清單。
# Python program to demonstrate # accessing of element from list # Creating a List with # the use of multiple values List = ["Geeks", "For", "Geeks"] # accessing a element from the # list using index number print("Accessing a element from the list") print(List[0]) print(List[2]) # Creating a Multi-Dimensional List # (By Nesting a list inside a List) List = [['Geeks', 'For'] , ['Geeks']] # accessing a element from the # Multi-Dimensional List using # index number print("Acessing a element from a Multi-Dimensional list") print(List[0][1]) print(List[1][0])
輸出:
Accessing a element from the listGeeksGeeksAcessing a element from a Multi-Dimensional listForGeeks
負索引
在Python中,負序列索引表示數組末尾的位置。不必像List[len(List)-3]那樣計算偏移量,隻需編寫List[-3]就足夠了。負索引意味着從結尾開始,-1表示最後一項,-2表示第二個最後項等。
List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks'] # accessing a element using # negative indexing print("Acessing element using negative indexing") # print the last element of list print(List[-1]) # print the third last element of list print(List[-3])
輸出:
Acessing element using negative indexingGeeksFor
從清單中删除元素
使用remove()方法
可以使用内置的remove()函數從清單中删除元素,但是如果元素不存在于集合中,則會發生錯誤。Remove()方法一次隻删除一個元素,要删除元素範圍,則使用疊代器。remove()方法删除指定的項目。
注意:清單中的Remove方法将隻删除搜尋到的元素的第一個比對項。
# Python program to demonstrate # Removal of elements in a List # Creating a List List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] print("Intial List: ") print(List) # Removing elements from List # using Remove() method List.remove(5) List.remove(6) print("List after Removal of two elements: ") print(List) # Removing elements from List # using iterator method for i in range(1, 5): List.remove(i) print("List after Removing a range of elements: ") print(List)
輸出:
Intial List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]List after Removal of two elements: [1, 2, 3, 4, 7, 8, 9, 10, 11, 12]List after Removing a range of elements: [7, 8, 9, 10, 11, 12]
使用pop()方法
Pop()函數還可以用于從集合中移除和傳回元素,但預設情況下,它隻移除集合的最後一個元素。若要從清單的特定位置移除元素,元素的索引将作為參數傳遞給Pop()方法。
List = [1,2,3,4,5] # Removing element from the # Set using the pop() method List.pop() print("List after popping an element: ") print(List) # Removing element at a # specific location from the # Set using the pop() method List.pop(2) print("List after popping a specific element: ") print(List)
輸出:
List after popping an element: [1, 2, 3, 4]List after popping a specific element: [1, 2, 4]
清單切片(Slicing of a List)
在Python List中,有多種方法可以列印包含所有元素的整個清單,但是要從清單中列印特定範圍的元素,我們使用Slice操作。切片操作在使用冒号(:)的清單上執行。若要從開始到範圍列印元素,請使用[:Index];若要從結束使用[:-Index]列印元素;若要從特定索引列印元素,直到結束使用[Index:];若要列印範圍内的元素,請使用[開始索引:結束索引];若要使用切片操作列印整個清單,請使用[:]。此外,要按相反順序列印整個清單,請使用[::-1]。
注意:要從後端列印清單元素,請使用負索引。
# Python program to demonstrate # Removal of elements in a List # Creating a List List = ['G','E','E','K','S','F', 'O','R','G','E','E','K','S'] print("Intial List: ") print(List) # Print elements of a range # using Slice operation Sliced_List = List[3:8] print("Slicing elements in a range 3-8: ") print(Sliced_List) # Print elements from a # pre-defined point to end Sliced_List = List[5:] print("Elements sliced from 5th " "element till the end: ") print(Sliced_List) # Printing elements from # beginning till end Sliced_List = List[:] print("Printing all elements using slice operation: ") print(Sliced_List)
輸出:
Intial List: ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']Slicing elements in a range 3-8: ['K', 'S', 'F', 'O', 'R']Elements sliced from 5th element till the end: ['F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']Printing all elements using slice operation: ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
負索引清單切片
# Creating a List List = ['G','E','E','K','S','F', 'O','R','G','E','E','K','S'] print("Initial List: ") print(List) # Print elements from beginning # to a pre-defined point using Slice Sliced_List = List[:-6] print("Elements sliced till 6th element from last: ") print(Sliced_List) # Print elements of a range # using negative index List slicing Sliced_List = List[-6:-1] print("Elements sliced from index -6 to -1") print(Sliced_List) # Printing elements in reverse # using Slice operation Sliced_List = List[::-1] print("Printing List in reverse: ") print(Sliced_List)
輸出:
Initial List: ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']Elements sliced till 6th element from last: ['G', 'E', 'E', 'K', 'S', 'F', 'O']Elements sliced from index -6 to -1['R', 'G', 'E', 'E', 'K']Printing List in reverse: ['S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G']
清單方法
帶清單的内置函數