新聞中心
在 Python 中,列表是可變的序列類型。列表對(duì)象在方括號(hào)[]中包含一個(gè)或多個(gè)不同數(shù)據(jù)類型的項(xiàng),用逗號(hào)分隔。下面聲明了 lists 變量。

為平度等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及平度網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、成都網(wǎng)站建設(shè)、平度網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
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)
根據(jù)計(jì)算機(jī)內(nèi)存的限制,列表可以包含無限的數(shù)據(jù)。
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]
可以使用方括號(hào)[]中從零開始的索引來訪問列表項(xiàng)。索引從零開始,每個(gè)項(xiàng)目遞增 1。使用比列表項(xiàng)目總數(shù)更大的索引訪問項(xiàng)目會(huì)導(dǎo)致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
一個(gè)列表可以包含多個(gè)內(nèi)部列表,作為可以使用索引訪問的項(xiàng)。
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]
列表類
所有列表對(duì)象都是 Python 中list類的對(duì)象。使用list()構(gòu)造器將元組、集合、字典、字符串等其他序列類型轉(zhuǎn)換為列表。
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:
['H', 'e', 'l', 'l', 'o']
[1, 2]
[10, 20, 30]
[100, 200, 300]
迭代列表
使用循環(huán)的可以迭代列表項(xiàng)。
names=["Jeff", "Bill", "Steve", "Mohan"]
for name in names:
print(name)
Output:
Jeff
Bill
Steve
Mohan
更新列表
列表是可變的。您可以使用append()或insert()方法在列表中添加新項(xiàng)目,并使用索引更新項(xiàng)目。
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"]
請(qǐng)注意,如果指定索引處的元素不存在,將引發(fā)錯(cuò)誤“索引超出范圍”。
移除項(xiàng)目
使用remove()、pop()方法或del關(guān)鍵字刪除列表項(xiàng)或整個(gè)列表。
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
列表運(yùn)算符
和字符串一樣,列表也是一個(gè)序列。因此,用于字符串的運(yùn)算符也可用于列表(以及元組)。
| 操作員 | 例子 |
|---|---|
| + 運(yùn)算符返回一個(gè)包含第一個(gè)和第二個(gè)列表所有元素的列表。 |
>>> L1=[1,2,3]
>>> L2=[4,5,6]
>>> L1+L2
[1, 2, 3, 4, 5, 6]
| | ***** 運(yùn)算符連接同一列表的多個(gè)副本。 |
>>> L1=[1,2,3]
>>> L1*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
| | 切片運(yùn)算符 [] 返回給定索引處的項(xiàng)目。負(fù)索引從右側(cè)開始計(jì)算位置。 |
>>> L1=[1, 2, 3]
>>> L1[0]
1
>>> L1[-3]
1
>>> L1[1]
2
>>> L1[-2]
2
>>> L1[2]
3
>>> L1[-1]
3
| | 范圍切片運(yùn)算符【從索引:直到索引-1】獲取由兩個(gè)索引操作數(shù)指定的范圍內(nèi)的項(xiàng)目,這兩個(gè)操作數(shù)用:符號(hào)分隔。 如果省略第一個(gè)操作數(shù),范圍從索引 0 開始。 如果省略第二個(gè)操作數(shù),范圍將上升到列表的末尾。 |
>>> 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]
| | 如果給定列表中存在某個(gè)項(xiàng)目,則運(yùn)算符中的返回真。 |
>>> L1=[1, 2, 3, 4, 5, 6]
>>> 4 in L1
True
>>> 10 in L1
False
| | 如果給定列表中不存在某個(gè)項(xiàng)目,則不在運(yùn)算符中的返回真。 |
>>> L1=[1, 2, 3, 4, 5, 6]
>>> 5 not in L1
False
>>> 10 not in L1
True
|
列出方法
| 列表方法 | 描述 |
|---|---|
| list.append() | 在列表末尾添加一個(gè)新項(xiàng)目。 |
| list.clear() | 從列表中移除所有項(xiàng)目并使其為空。 |
| list.copy() | 返回列表的淺拷貝。 |
| list.count() | 返回一個(gè)元素在列表中出現(xiàn)的次數(shù)。 |
| list.extend() | 將指定表的所有項(xiàng)目(列表、元組、集合、字典、字符串)添加到列表的末尾。 |
| list.index() | 返回指定項(xiàng)第一次出現(xiàn)的索引位置。如果找不到項(xiàng)目,則引發(fā)值錯(cuò)誤。 |
| list.insert() | 在給定位置插入項(xiàng)目。 |
| list.pop() | 從指定的索引位置返回一個(gè)項(xiàng)目,并將其從列表中移除。如果未指定索引,list.pop()方法將移除并返回列表中的最后一項(xiàng)。 |
| list.remove() | 從列表中刪除第一個(gè)出現(xiàn)的指定項(xiàng)目。如果找不到指定的項(xiàng)目,則會(huì)引發(fā) ValueError。 |
| list.reverse() | 反轉(zhuǎn)列表中元素的索引位置。第一個(gè)元素將位于最后一個(gè)索引處,第二個(gè)元素將位于第二個(gè)最后一個(gè)索引處,以此類推。 |
| list.sort() | 按升序、降序或自定義順序?qū)α斜眄?xiàng)進(jìn)行排序。 |
網(wǎng)站題目:Python列表
標(biāo)題URL:http://fisionsoft.com.cn/article/ccsjsij.html


咨詢
建站咨詢
