直接遍歷列表刪除
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] for el in l1: if l1.count(el) > 1: l1.remove(el) print(l1)# 会漏删,因为删除一个元素后,后面的元素向前补位,导致紧跟的一个元素被跳过.
透過遍歷索引刪除
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] for el in range(len(l1)): # 此时len(l1)已经确定,不会随着l1后面的变化而变化 if l1.count(l1[el]) > 1: l1.remove(l1[el]) print(l1) # 会报错,因为删除元素后导致l1的长度变短了,但是for遍历的还是之前的索引长度,会导致索引超过范围而报错
透過遍歷建立的切片來刪除原始清單
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] for el in l1[:]: if l1.count(el) > 1: l1.remove(el) # 没有问题,可以去重,但是无法保留原有的顺序 print(l1)
使用新列表記錄需要保留的元素
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] lst = [] for el in l1: if lst.count(el) < 1: lst.append(el) print(lst) # 没有问题,也能保留原有顺序,但是创建了新列表
透過索引倒著刪除
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] for el in range(len(l1)-1, -1, -1): if l1.count(l1[el]) > 1: l1.pop(el) # 没有问题,且保留原顺序 # l1.remove(l1[el]) # 没有问题,但是不能保留原有顺序 # del l1[el] # 这样则会保留原有顺序,小伙伴可以想一想为什么 print(l1)
透過遞歸函數刪除
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] def set_lst(lst): for el in lst: if lst.count(el) > 1: lst.remove(el) set_lst(lst) # 每次开辟一个新函数,判断上次被删除了一个元素后的列表 else: # 直到最后,列表里的元素都是一个,运行了else return lst print(set_lst(l1)) # 因为是从前面开始删除的,所以不保留原有顺序 ''' [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 3, 3, 3, 6, 6, 5, 5, 2, 2] [1, 3, 3, 6, 6, 5, 5, 2, 2] [1, 3, 6, 6, 5, 5, 2, 2] [1, 3, 6, 5, 5, 2, 2] [1, 3, 6, 5, 2, 2] [1, 3, 6, 5, 2] return lst = [1, 3, 6, 5, 2] '''
毫無疑問set()是最方便的
l1 = [1, 1, 2, 2, 3, 3, 3, 3, 6, 6, 5, 5, 2, 2] lst = list(set(l1)) print(lst)
以上是Python怎麼刪除清單中重複元素的詳細內容。更多資訊請關注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)

是的,YouCanconCatenatElistsusingAloopInpyThon.1)使用eparateLoopsForeachListToAppendIteMstoaresultList.2)useanestedlooptoiterateOverMultipliplipliplipliplipliplipliplipliplipliplistforamoreConciseApprace.3)

ThemostefficientmethodsforconcatenatinglistsinPythonare:1)theextend()methodforin-placemodification,2)itertools.chain()formemoryefficiencywithlargedatasets.Theextend()methodmodifiestheoriginallist,makingitmemory-efficientbutrequirescautionifpreserving

pythonboopsincludeforandwhileloops,with forloopsidealforequencessand and whileloopsforcondition repetition.bestpracticesinvolve:1)使用listComprehensionsforshensionsforsimpletranspletransformations,2)obseringEnumerateForIndex-valuepairs,3)optingftingftingfortermornemoremoremoremore


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

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