python中关于删除list中的某个元素,一般有三种方法:remove、pop、del:
1.remove: 删除单个元素,删除首个符合条件的元素,按值删除
举例说明:
>>> str=[1,2,3,4,5,2,6] >>> str.remove(2) >>> str [1, 3, 4, 5, 2, 6]
2.pop: 删除单个或多个元素,按位删除(根据索引删除)
>>> str=[0,1,2,3,4,5,6] >>> str.pop(1) #pop删除时会返回被删除的元素 >>> str [0, 2, 3, 4, 5, 6] >>> str2=['abc','bcd','dce'] >>> str2.pop(2) 'dce' >>> str2 ['abc', 'bcd']
3.del:它是根据索引(元素所在位置)来删除
举例说明:
>>> str=[1,2,3,4,5,2,6] >>> str2.pop(2) [1, 3, 4, 5, 2, 6] >>> str2=['abc','bcd','dce'] >>> del str2[1] >>> str2 ['abc', 'dce']
除此之外,del还可以删除指定范围内的值。
#Python学习交流QQ群:778463939 >>> str=[0,1,2,3,4,5,6] >>> del str[2:4] #删除从第2个元素开始,到第4个为止的元素(但是不包括尾部元素) >>> str [0, 1, 4, 5, 6]
del 也可以删除整个数据对象(列表、集合等)
>>> str=[0,1,2,3,4,5,6] >>> del str >>> str #删除后,找不到对象 Traceback (most recent call last): File "<pyshell#27>", line 1, in <module> str NameError: name 'str' is not defined
注意:del是删除引用(变量)而不是删除对象(数据),对象由自动垃圾回收机制(GC)删除。
补充: 删除元素的变相方法
print('s1_2:', s1) 's3:', s3) s1 = (1, 2, 3, 4, 5, 6) s2 = (2, 3, 5) s3 = [] for i in s1: if i not in s2: s3.append(i) print('s1_1:', s1) s1 = s3 print('s2:', s2) print('s3:', s3) print('s1_2:', s1)
以上是Python教程:删除列表元素的三种方法的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

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)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3汉化版
中文版,非常好用

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具