Python列表與數組不同。在處理數組時,我們討論了一組同類數據元素。對於python中的列表,情況並非如此。Python List可以存儲異構的元素集合。此功能將幫助開發人員和程序員以更靈活的方式處理列表。python中的List是最強大的內置數據結構之一。
python中的列表還可以存儲整數,浮點值,字符串,布爾值和複雜值。
如何在python中創建一個List
我們可以用兩種方式在python中創建一個list
- 通過聲明一個帶有空方括號的變量 i.e []
- 通過使用list()。
例
Python學習交流群:1004391443,這裡是python學習者聚集地,有大牛答疑,有資源共享!小編也準備了一份python學習資料,有想學習python編程的,或是轉行,或是大學生,還有工作中想提升自己能力的,正在學習的小夥伴歡迎加入學習。
# Here first I'm creating a my todo list which is used to store my to-do activities.
myTODOList = []
# The above line will create a list object for me
# I'm creating a another list which will store my general information.
myGeneralInfo = list()
# The above line will also create a list object for me
# Getting the types of list objects
print(type(myTODOList))
print(type(myGeneralInfo))
輸出
您可以使用最常用的方法創建新的列表對象。現在我們將繼續討論如何在列表中添加新元素以及更多內容。
如何將數據添加到列表?
首先,我想介紹一下Mutability的概念。可變性意味著改變其行為的能力。Python列表本質上是可變的。我們可以在列表中添加或刪除元素。與其他內置數據結構相比,這是吸引程序員使用列表的最大優勢之一。
我們可以通過兩種方式向列表添加元素:
- 通過使用append()
- 通過使用insert()
通過使用append()
藉助append方法,我們可以一次添加一個元素。此方法將幫助我們僅在列表的末尾添加元素。
append函數的語法是 -
listName.append(項目/元件)
# Adding Elements to the lists
myTODOList.append('Wake up Early Morning')
myTODOList.append('Go to Gym')
myTODOList.append('Play Some Games')
myTODOList.append('Get ready to go the college')
myTODOList.append('Go to library')
# Printing the entire list elements
print(myTODOList)
輸出
通過使用insert()
此插入方法用於在給定列表中的指定位置添加元素。
insert函數的語法是 -
listName.insert(position,item / element)
Insert()使用兩個參數 - position和list item。該位置是元素需要保留在列表中的位置。這些位置通常稱為索引。通常,python中的列表索引從0開始。(即第一個元素索引為0,第二個元素為1,第三個元素索引為2,依此類推)。由此,我們可以得出結論:
n個元素的列表最多具有n-1的索引號,即具有5個元素的列表將具有最大索引值4。
例
# Adding Elements to our list with the help of insert()
myGeneralInfo.insert(0, 'Paid the Library Fee')
myGeneralInfo.insert(1, 12000)
myGeneralInfo.insert(2, True)
myGeneralInfo.insert(3, 14+12j)
myGeneralInfo.insert(4, 3.141521)
# Printing the myGeneralInfo list information
print(myGeneralInfo)
輸出
如何訪問列表元素
我們可以使用以下兩種方式訪問元素列表:
- 通過使用索引運算符。
- 通過使用切片運算符
通過使用索引運算符
我們可以在索引運算符的幫助下直接訪問列表元素。
例
# Acessing the certain values from the list
print(myTODOList[1])
print(myTODOList[3])
print(myTODOList[4])
輸出
通過使用切片運算符
切片運算符是有效訪問列表元素的最常用運算符之一。slice運算符的語法是:
listName [start:stop:step]
start - 它表示切片必須開始的索引。默認值為0。
stop - 它表示切片必須結束的索引。默認值是列表的最大允許索引,即列表的長度。
step - 增加值。默認值為1。
例
# Getting the information using slice operator
print(myTODOList[0:3]) # we don't need to specify the step value.
print(myTODOList[2:4:1])
print(myTODOList[0:4:2])
輸出
Python列表是可迭代的對象。對於python中的任何可迭代對象,我們可以編寫for循環來打印出所有數據。
例
# Iterating over the list
for item in myGeneralInfo:
print(item)
如何從列表中刪除元素
我們可以通過以下兩種方式刪除列表元素:
- 通過使用remove()
- 通過使用pop()
通過使用remove()
remove()用於刪除指定給它的元素。remove()的語法是:
listName.remove(項目/元件)
# Deleting the element from the list
myGeneralInfo.remove(12000)
myGeneralInfo.remove('Paid the Library Fee')
# printing the result after deleting the elements
print(myGeneralInfo)
通過使用pop()
它是一個迭代器方法,用於一次刪除單個(或)多個元素。它從背面刪除元素。pop()方法的語法是:
listName.pop()
# printing the list items before deleting
print('My TODO List Elements: ',myTODOList)
print('My General list Elements: ',myGeneralInfo)
# Deleting the list elements using pop()
myTODOList.pop()
myTODOList.pop()
# Deleting the list elements completely
for item in range(len(myGeneralInfo)):
myGeneralInfo.pop()
# printing the results
print('My TODO List Elements: ',myTODOList)
print('My General list Elements: ',myGeneralInfo)
在上面的程序中,我們在for循環中使用了len () 。len () 用於給出列表的長度,即列表中存在的元素的數量。
列表對象上的各種屬性和函數
python dir()函數用於提供與之關聯的內置屬性和方法集。
例
# Printing all the attributes and functions on the list object
print(dir(myTODOList))
輸出
各種列表方法及其用途:
1. append () - 它會在列表末尾添加一個元素。
2. clear () - 用於從列表中刪除所有項目。
3. copy () - 用於返回列表的另一個副本。
4. count () - 用於返回作為參數傳遞的項數的計數。
5. extend () - 它將列表的所有元素添加到另一個列表中。
6. index () - 用於返回第一個匹配項的索引。
7. insert () - 用於在定義的索引處插入項目。
8. pop () - 用於刪除和返回給定索引處的元素。
9. remove () - 用於從列表中刪除項目。
10. reverse () - 用於反轉列表中項目的順序。
11. sort () - 用於按升序對列表中的項目進行排序。
何時使用列表數據結構?
如果要存儲多個數據對象,則必須保留插入順序。如果您還想存儲重複值,那麼此數據結構將更有助於執行此類操作。