在 Python 中,列表是可變的序列類型。列表對象在方括號[]中包含一個或多個不同數據類型的項,用逗號分隔。下面聲明了 lists 變量。
mylist=[] # empty list
print(mylist)
names=["Jeff", "Bill", "Steve", "Mohan"] # string list
print(names)
item=[1, "Jeff", "Computer", 75.50, True] # list with heterogeneous data
print(item)
根據計算機內存的限制,列表可以包含無限的數據。
nums=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60]
可以使用方括號[]中從零開始的索引來訪問列表項。索引從零開始,每個項目遞增 1。使用比列表項目總數更大的索引訪問項目會導致IndexError
。
names=["Jeff", "Bill", "Steve", "Mohan"]
print(names[0]) # returns "Jeff"
print(names[1]) # returns "Bill"
print(names[2]) # returns "Steve"
print(names[3]) # returns "Mohan"
print(names[4]) # throws IndexError: list index out of range
一個列表可以包含多個內部列表,作為可以使用索引訪問的項。
nums=[1, 2, 3, [4, 5, 6, [7, 8, [9]]], 10]
print(names[0]) # returns 1
print(names[1]) # returns 2
print(names[3]) # returns [4, 5, 6, [7, 8, [9]]]
print(names[4]) # returns 10
print(names[3][0]) # returns 4
print(names[3][3]) # returns [7, 8, [9]]
print(nums[3][3][0]) # returns 7
print(nums[3][3][2]) # returns [9]
Output:
1
2
[4, 5, 6, [7, 8, [9]]]
10
4
[7, 8, [9]]
7
[9]
列表類
所有列表對象都是 Python 中list
類的對象。使用list()
構造器將元組、集合、字典、字符串等其他序列類型轉換為列表。
nums=[1,2,3,4]
print(type(nums))
mylist=list('Hello')
print(mylist)
nums=list({1:'one',2:'two'})
print(nums)
nums=list((10, 20, 30))
print(nums)
nums=list({100, 200, 300})
print(nums)
Output:
<class 'list'>
['H', 'e', 'l', 'l', 'o']
[1, 2]
[10, 20, 30]
[100, 200, 300]
迭代列表
使用循環的可以迭代列表項。
names=["Jeff", "Bill", "Steve", "Mohan"]
for name in names:
print(name)
Output:
Jeff
Bill
Steve
Mohan
更新列表
列表是可變的。您可以使用append()
或insert()
方法在列表中添加新項目,并使用索引更新項目。
names=["Jeff", "Bill", "Steve", "Mohan"]
names[0]="Newton" # update 1st item at index 0
names[1]="Ram" # update 2nd item at index 1
names.append("Abdul") # adds new item at the end
print(names)
Output:
["Newton", "Ram", "Steve", "Mohan", "Abdul"]
請注意,如果指定索引處的元素不存在,將引發錯誤“索引超出范圍”。
移除項目
使用remove()
、pop()
方法或del
關鍵字刪除列表項或整個列表。
names=["Jeff", "Bill", "Steve", "Mohan"]
del names[0] # removes item at index 0
print("After del names[0]: ", names)
names.remove("Bill") # removes "Bill"
print("After names.remove("Bill"): ", names)
print(names.pop(0)) # return and removes item at index 0
print("After names.pop(0): ", names)
names.pop() # return removes item at last index
print("After names.pop(): ", names)
del names # removes entire list object
print(names)
Output:
After del names[0]: ["Bill", "Steve", "Mohan"]
After names.remove("Bill"): ["Steve", "Mohan"]
"Steve"
After names.pop(0):["Mohan"]
"Mohan"
After names.pop(): []
NameError: name 'names' is not defined
列表運算符
和字符串一樣,列表也是一個序列。因此,用于字符串的運算符也可用于列表(以及元組)。
操作員 | 例子 |
---|---|
+ 運算符返回一個包含第一個和第二個列表所有元素的列表。 |
>>> L1=[1,2,3]
>>> L2=[4,5,6]
>>> L1+L2
[1, 2, 3, 4, 5, 6]
| | ***** 運算符連接同一列表的多個副本。 |
>>> L1=[1,2,3]
>>> L1*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
| | 切片運算符 [] 返回給定索引處的項目。負索引從右側開始計算位置。 |
>>> L1=[1, 2, 3]
>>> L1[0]
1
>>> L1[-3]
1
>>> L1[1]
2
>>> L1[-2]
2
>>> L1[2]
3
>>> L1[-1]
3
| | 范圍切片運算符【從索引:直到索引-1】獲取由兩個索引操作數指定的范圍內的項目,這兩個操作數用:符號分隔。 如果省略第一個操作數,范圍從索引 0 開始。 如果省略第二個操作數,范圍將上升到列表的末尾。 |
>>> L1=[1, 2, 3, 4, 5, 6]
>>> L1[1:]
[2, 3, 4, 5, 6]
>>> L1[:3]
[1, 2, 3]
>>> L1[1:4]
[2, 3, 4]
>>> L1[3:]
[4, 5, 6]
>>> L1[:3]
[1, 2, 3]
>>> L1[-5:-3]
[2, 3]
| | 如果給定列表中存在某個項目,則運算符中的返回真。 |
>>> L1=[1, 2, 3, 4, 5, 6]
>>> 4 in L1
True
>>> 10 in L1
False
| | 如果給定列表中不存在某個項目,則不在運算符中的返回真。 |
>>> L1=[1, 2, 3, 4, 5, 6]
>>> 5 not in L1
False
>>> 10 not in L1
True
|
列出方法
列表方法 | 描述 |
---|---|
list.append() | 在列表末尾添加一個新項目。 |
list.clear() | 從列表中移除所有項目并使其為空。 |
list.copy() | 返回列表的淺拷貝。 |
list.count() | 返回一個元素在列表中出現的次數。 |
list.extend() | 將指定表的所有項目(列表、元組、集合、字典、字符串)添加到列表的末尾。 |
list.index() | 返回指定項第一次出現的索引位置。如果找不到項目,則引發值錯誤。 |
list.insert() | 在給定位置插入項目。 |
list.pop() | 從指定的索引位置返回一個項目,并將其從列表中移除。如果未指定索引,list.pop()方法將移除并返回列表中的最后一項。 |
list.remove() | 從列表中刪除第一個出現的指定項目。如果找不到指定的項目,則會引發 ValueError。 |
list.reverse() | 反轉列表中元素的索引位置。第一個元素將位于最后一個索引處,第二個元素將位于第二個最后一個索引處,以此類推。 |
list.sort() | 按升序、降序或自定義順序對列表項進行排序。 |