搜尋

Python 列表(List)

Nov 23, 2016 am 10:41 AM
python

序列是Python中最基本的資料結構。序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推。

Python有6個序列的內建類型,但最常見的是清單和元組。

序列都可以進行的操作包括索引,切片,加,乘,檢查成員。

此外,Python已經內建確定序列的長度以及確定最大和最小的元素的方法。

清單是最常用的Python資料類型,它可以作為一個方括號內的逗號分隔值出現。

列表的資料項不需要具有相同的類型

建立一個列表,只要把逗號分隔的不同的資料項使用方括號括起來即可。如下圖所示:

list1 = ['physics', 'chemistry', 1997, 2000];

, "b", "c", "d"];

   

 

與字串的索引一樣,清單索引從0開始。列表可以進行截取、組合等。

存取清單中的值

使用下標索引來存取清單中的值,同樣你也可以使用方括號的形式截取字符,如下所示:

on

#!/usr/bin/python

 

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = "list1[0] : ", list1[0]

print "list2[1:5]: ", list2[1:5]

   

7 physics

list2[1:5]:  [2, 3, 4, 5]

   

 

.方法新增清單項目,如下圖:

#!/usr/bin/python

 

list = ['physics', 'chemistry', Value available at index 2 : "

print list[2];

list[2] = 2001;

print "New value 

注意:我們會在接下來的章節討論append()方法的使用

以上實例輸出結果:

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:

   

 

以上實例輸出結果:

['physics', 'chemistry', 1997, 2000]

🠎@@🠎ph delo. 'chemistry', 2000]

   

 

注意:我們會在接下來的章節討論remove()方法的使用

Python列表腳本操作符

列表對+ 和* 的操作符與字串相似。 + 號用於組合列表,* 號用於重複列表。

如下所示:

Python 表達式

結果

描述

len([1, 2, 3])    3    長度   

[1, 2, 3] + [4, 5, 6]    [1, 2, 3, 4, 5, 6]    組合   

['Hi!'] * 4    ['Hi!', 'Hi!', 'Hi!', 'Hi!']    ['Hi!', 'Hi!', 'Hi!', 'Hi!']    ['Hi!', 'Hi!', 'Hi!', 'Hi!']   重複重複3 in [1, 2, 3]    True    元素是否存在於列表中   

for x in [1, 2, 3]: print x,    1 2 3   對迭代中操作類型,如下圖:

L = ['spam', 'Spam', 'SPAM!']

   

動作

式運算

結果

描述

L[2]    'SPAM!'    讀取清單中第三個元素   

L[-2]     

L[1:]    ['Spam', 'SPAM!']    從第二個元素開始截取列表   

Python列表函數&方法

Python 1    cmp(list1, list2)

比較兩個清單的元素   

2    len(list)

列表元素個數   

3    max(list)個數   

3    max(list)個數字列表最小值   

5    list(seq)
將元組轉換為列表   

Python包含以下方法:


序號

)清單新的對象


2    list.count(obj)

統計某個元素在列表中出現的次數   


3    list.extend(seq)

在列表末尾一次性追加另一個序列中擴展的多個值(用新列表原來的清單)    

4    list.index(obj)

從清單中找出某值第一個符合項目的索引位置   

5    list.insert pop([index])

移除清單中的一個元素(預設最後一個元素),並且傳回該元素的值   

7    list.remove(obj)

移除清單中某個值的第一個符合項目

8    list.reverse()
反向清單中元素   

9    list.sort([func])
將原始清單排序   


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
可以在Python數組中存儲哪些數據類型?可以在Python數組中存儲哪些數據類型?Apr 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python標準庫的哪一部分是:列表或數組?Python標準庫的哪一部分是:列表或數組?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

您應該檢查腳本是否使用錯誤的Python版本執行?您應該檢查腳本是否使用錯誤的Python版本執行?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),