cari
Rumahpembangunan bahagian belakangTutorial PythonPython 列表(List)操作方法详解

列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型。列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。
Python有6个序列的内置类型,但最常见的是列表和元组。序列都可以进行的操作包括索引,切片,加,乘,检查成员。此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法。

一、创建一个列表
只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。
二、访问列表中的值
使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符,如下所示:

#!/usr/bin/python

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()方法来添加列表项,如下所示:

#!/usr/bin/python
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2];
list[2] = 2001;
print "New value available at index 2 : "
print list[2];

以上实例输出结果:

Value available at index 2 :
1997
New value available at index 2 :
2001

四、删除列表元素
可以使用 del 语句来删除列表的的元素,如下实例:

#!/usr/bin/python
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列表脚本操作符
列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。

如下所示: 

Python 列表(List)操作方法详解

六、Python列表截取
Python的列表截取与字符串操作类型,如下所示:

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

操作:

Python 列表(List)操作方法详解

七、Python列表操作的函数和方法
列表操作包含以下函数:
1、cmp(list1, list2):比较两个列表的元素 
2、len(list):列表元素个数 
3、max(list):返回列表元素最大值 
4、min(list):返回列表元素最小值 
5、list(seq):将元组转换为列表 
列表操作包含以下方法:
1、list.append(obj):在列表末尾添加新的对象
2、list.count(obj):统计某个元素在列表中出现的次数
3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
5、list.insert(index, obj):将对象插入列表
6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7、list.remove(obj):移除列表中某个值的第一个匹配项
8、list.reverse():反向列表中元素
9、list.sort([func]):对原列表进行排序

更多Python 列表(List)操作方法详解相关文章请关注PHP中文网!

Kenyataan
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Python: menyelam mendalam ke dalam kompilasi dan tafsiranPython: menyelam mendalam ke dalam kompilasi dan tafsiranMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation: 1) thepythoninterpretercompilessourcodcecodeintoplatform-independentbytecode.2) thepythonvirtualmachine (PVM) thenexecutesthisbytecode, BalantingeaseOfusoWithperformance.

Adakah Python diterjemahkan atau bahasa yang disusun, dan mengapa ia penting?Adakah Python diterjemahkan atau bahasa yang disusun, dan mengapa ia penting?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandandcompiled.1) it'scompiledtobytecodeforporabilityAcrossplatforms.2) theBytecodeistheninterpreted, membolehkanfordynamictypingandrapiddevelopment, walaupunItmayBeslowerLowerWanLelyCiledlanguages.

Untuk gelung vs semasa gelung di python: perbezaan utama dijelaskanUntuk gelung vs semasa gelung di python: perbezaan utama dijelaskanMay 12, 2025 am 12:08 AM

ForloopsareidealwhenyonesshenumberofiterationsationseSinadvance, whilewhileloopsarebetterforsituationshipheryouneedtoloopuntilaconditionismet.forloopsaremoreeficientablyandable, yang sesuai, manakala whileloopsoffermorecontrolandareusefereficeficeficeficeficient,

Untuk dan semasa gelung: panduan praktikalUntuk dan semasa gelung: panduan praktikalMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance, whilewhileloopsareusedwhenTheiterationsdependonacondition.1) forloopsareidealforiteratingoversequencesLikeListsorArrays.2)

Python: Adakah ia benar -benar ditafsirkan? Membebaskan mitosPython: Adakah ia benar -benar ditafsirkan? Membebaskan mitosMay 12, 2025 am 12:05 AM

Pythonisnotpurelyinterinterpreted; itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1) pythoncompilessourcecodeintobytecode, whoomeSthenexecutedbythepythonvirtualmachine (pvm)

Senarai concatenate python dengan elemen yang samaSenarai concatenate python dengan elemen yang samaMay 11, 2025 am 12:08 AM

ToConcatenatelistsinpythonwiththesameelements, gunakan: 1) operatortokokduplicates, 2) asettoremoveduplicates, OR3) listomprehensionfensionfensionfensionfensiontroloverduplicates, setiapmethodhasdifferentperformanceAdordlications.

Ditafsirkan vs bahasa yang disusun: Tempat PythonDitafsirkan vs bahasa yang disusun: Tempat PythonMay 11, 2025 am 12:07 AM

Pythonisaninterpretedlanguage, menawarkanfuseofuseandflexibilitybutfacingperpormancelimitationsincriticalapplications.1) interpretlanguagesepythonexecuteline-by-line, membolehkanMmediateDebackandrapidprototyping.2)

Untuk dan semasa gelung: Bilakah anda menggunakan setiap python?Untuk dan semasa gelung: Bilakah anda menggunakan setiap python?May 11, 2025 am 12:05 AM

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

See all articles

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Nordhold: Sistem Fusion, dijelaskan
3 minggu yang laluBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers of the Witch Tree - Cara Membuka Kunci Cangkuk Bergelut
3 minggu yang laluBy尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

SublimeText3 Linux versi baharu

SublimeText3 Linux versi baharu

SublimeText3 Linux versi terkini

Versi Mac WebStorm

Versi Mac WebStorm

Alat pembangunan JavaScript yang berguna