日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python列表

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

為平度等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及平度網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、成都網(wǎng)站建設(shè)、平度網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

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ù)計算機內(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] 

可以使用方括號[]中從零開始的索引來訪問列表項。索引從零開始,每個項目遞增 1。使用比列表項目總數(shù)更大的索引訪問項目會導(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 

一個列表可以包含多個內(nèi)部列表,作為可以使用索引訪問的項。

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()構(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)的可以迭代列表項。

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"] 

請注意,如果指定索引處的元素不存在,將引發(fā)錯誤“索引超出范圍”。

移除項目

使用remove()、pop()方法或del關(guān)鍵字刪除列表項或整個列表。

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]

| | 切片運算符 [] 返回給定索引處的項目。負索引從右側(cè)開始計算位置。 |

>>> L1=[1, 2, 3]
>>> L1[0] 
1                  
>>> L1[-3]
1
>>> L1[1] 
2
>>> L1[-2]
2
>>> L1[2]
3
>>> L1[-1] 
3 

| | 范圍切片運算符【從索引:直到索引-1】獲取由兩個索引操作數(shù)指定的范圍內(nèi)的項目,這兩個操作數(shù)用:符號分隔。 如果省略第一個操作數(shù),范圍從索引 0 開始。 如果省略第二個操作數(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]

| | 如果給定列表中存在某個項目,則運算符中的返回真。 |

>>> 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() 返回一個元素在列表中出現(xiàn)的次數(shù)。
list.extend() 將指定表的所有項目(列表、元組、集合、字典、字符串)添加到列表的末尾。
list.index() 返回指定項第一次出現(xiàn)的索引位置。如果找不到項目,則引發(fā)值錯誤。
list.insert() 在給定位置插入項目。
list.pop() 從指定的索引位置返回一個項目,并將其從列表中移除。如果未指定索引,list.pop()方法將移除并返回列表中的最后一項。
list.remove() 從列表中刪除第一個出現(xiàn)的指定項目。如果找不到指定的項目,則會引發(fā) ValueError。
list.reverse() 反轉(zhuǎn)列表中元素的索引位置。第一個元素將位于最后一個索引處,第二個元素將位于第二個最后一個索引處,以此類推。
list.sort() 按升序、降序或自定義順序?qū)α斜眄椷M行排序。

分享文章:Python列表
URL地址:http://www.dlmjj.cn/article/ccsjsij.html