1.何為列表
1.1 列表作為序列(sequence)的一種,是一組有順序的元素的集合。
1.2 清單是Python中最常用的內建資料類型,用中括號[元素1,元素2...]括起,以逗號分隔,元素之間沒有任何關係,可以是任何類型。
2.清單的宣告與存取
#!/usr/bin/python# -*- coding:utf-8 -*- #变量的声明market = ['Apple','Banana','computer']#打印列表元素print market[0],market[1],market[2],market[-1].title()#For循环打印列表元素for element in market:print element,element.title()
3.清單中元素的修改、新增與刪除
3.1 修改清單元素,清單名稱+對應元素的索引
#修改索引值为2,即第3个元素值 market[2] = 'Telephone' print market #打印结果:['Apple', 'Banana', 'Telephone']
3.2 列表中加入元素,Python提供了append()和insert()方法,append()表示在列表的末尾添加元素,insert()可以指定列表的位置加入元素,如:
market.append('Orange') print market #得到结果:['Apple', 'Banana', 'computer', 'Orange'] market.insert(1,'Watermelon') print market #得到结果:['Apple', 'Watermelon', 'Banana', 'computer', 'Orange']
3.3 清單中刪除元素,可以使用del語句、pop()和remove()方法,如:
#删除第1个元素Apple del market[0] print market #pop()方法删除列表末尾元素,可以接着使用它赋给其它的列表,如: pop_market = market.pop() #此时把通过pop()方法弹出的末尾元素赋给新的变量pop_market print type(pop_market) #通过打印pop_market的类型得知,此时类型为String <type> #如果想让弹出的元素赋值给新的列表该怎么办呢,可以先声明列表,然后直接用 append()方法追加,如下: pop_list_p = [] pop_list_p.append(market.pop()) print pop_list_p #或列表的切片,后续会提到 pop_list = market[-1] print market print pop_market print pop_list #remove()方法从列表中删除元素时,也可以接着使用它的值: #使用remove()方法时,只需要制定元素对应的值即可,如:删除元素Watermelon market.remove('Watermelon') print market</type>
4.列表的組織,sort()方法表示永久排序,sorted()函數表示暫時性排序,reverse()方法表示反轉列表元素排列順序,len()函數表示列表長度
market = ['Computer','Banana','Apple'] market.sort()print market #sort()方法永久性改变了列表的元素排列的顺序,结果:['Apple', 'Banana', 'Computer']#sorted()函数临时性改变了列表的元素排列的顺序market = ['Computer','Banana','Apple']print (sorted(market)) #临时性排序['Apple', 'Banana', 'Computer']print market #再次打印还是预先的顺序['Computer', 'Banana', 'Apple']market.reverse()print market #reverse()方法反转列表元素的顺序#确定列表元素长度,也即是列表包含的元素个数,注意在用len()函数统计列表元素时是从1开始的print len(market)
關於列表的一些常用運算:
使用下標索引來存取清單中的值,同樣你也可以使用方括號的形式截取字符,如下:
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print("list1[0]: ", list1[0])
#print("list2[1: 5]: ", list2[1:5])
輸出結果
list1[0]: physics
list2[1:5] : [2, 3, 4, 5]
更新清單
可修改清單的資料項目或更新,也可使用append()方法來新增清單項目
list = ['physics', 'chemistry', 1997, 2000]
#
print("Value available at index 2 :
## #print("Value available at index 2: ")print(list[2])list[2] = 2001print("New value available at index 2 : ")print(list[2]) 輸出結果:#Value available at index 2 :1997#New value available at index 2 :2001 刪除清單元素使用del 語句來刪除清單的的元素list1 = ['physics', 'chemistry', 1997, 2000]
##
print(list1)
del list1[2]
print("After deleting value at index 2 : ")
print(list1)
#以上實例輸出結果:
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
Python清單腳本運算子
清單對+ 和* 的運算子與字串相似。 + 號用於組合列表,* 號用於重複列表
len([1, 2, 3])
>>>3
[1, 2, 3] + [4, 5, 6]
>>>[1, 2, 3, 4, 5, 6]
['Hi!'] * 4
#>>>['Hi!', 'Hi!', 'Hi!', 'Hi !']
3 in [1, 2, 3]
>>>True
#
#for x in [1, 2, 3]:
print(x)
>>>123
#Python列表截取
Python的列表截取與字串操作類型,如下所示
L = ['spam', 'Spam', 'SPAM!' ]
L[2] #讀取清單中第三個元素>>>SPAM!# L[-2] #讀取清單中倒數第二個元素#>>>Spam
##L[1:] #從第二個元素開始截取清單
以上是Python列表List的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版
好用的JavaScript開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具