婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Python中移除List重復項的五種方法

Python中移除List重復項的五種方法

熱門標簽:外呼系統哪些好辦 沈陽外呼系統呼叫系統 武漢外呼系統平臺 池州外呼調研線路 沈陽防封電銷卡品牌 如何申請400電話費用 沈陽人工外呼系統價格 江西省地圖標注 富錦商家地圖標注

 本文列些處幾種去除在Python 列表中(list)可能存在的重復項,這在很多應用程序中都會遇到的需求,作為程序員最好了解其中的幾種方法 以備在用到時能夠寫出有效的程序。

方法1:樸素方法

這種方式是在遍歷整個list的基礎上,將第一個出現的元素添加在新的列表中。

示例代碼:

# Python 3 code to demonstrate 
# removing duplicated from list 
# using naive methods 
  
# initializing list
test_list = [1, 3, 5, 6, 3, 5, 6, 1]
print ("The original list is : " +  str(test_list))
  
# using naive method
# to remove duplicated 
# from list 
res = []
for i in test_list:
    if i not in res:
        res.append(i)
  
# printing list after removal 
print ("The list after removing duplicates : " + str(res))

→ 輸出結果:
The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The list after removing duplicates : [1, 3, 5, 6]

方法2:列表解析式

這種方式實際上是第一種方法的簡化版,它利用列表解析式,使用一行代碼就可以替代上面的循環方式。

示例代碼:

# Python 3 code to demonstrate 
# removing duplicated from list 
# using list comprehension
  
# initializing list
test_list = [1, 3, 5, 6, 3, 5, 6, 1]
print ("The original list is : " +  str(test_list))
  
# using list comprehension
# to remove duplicated 
# from list 
res = []
[res.append(x) for x in test_list if x not in res]
  
# printing list after removal 
print ("The list after removing duplicates : " + str(res))

→ 輸出結果:
The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The list after removing duplicates : [1, 3, 5, 6]

方法3:使用set()

這種方式是最流行的方法來去除列表中的重復元素。但該方法的最大的一個缺點就是使用過后列表中元素的順序不再繼續保持與原來一致了。

示例代碼:

# Python 3 code to demonstrate 
# removing duplicated from list 
# using set()
  
# initializing list
test_list = [1, 5, 3, 6, 3, 5, 6, 1]
print ("The original list is : " +  str(test_list))
  
# using set()
# to remove duplicated 
# from list 
test_list = list(set(test_list))
  
# printing list after removal 
# distorted ordering
print ("The list after removing duplicates : " + str(test_list))

→ 輸出結果:
The original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The list after removing duplicates : [1, 3, 5, 6]

方法4:利用列表解析式 + enumerate()

該方法是在列表解析式的基礎上利用枚舉來去除重復元素。通過檢查元素是否已經在列表中存在從而將其略過。這種方法可以保持列表中的元素順序不會改變。

示例代碼:

# Python 3 code to demonstrate 
# removing duplicated from list 
# using list comprehension + enumerate()
  
# initializing list
test_list = [1, 5, 3, 6, 3, 5, 6, 1]
print ("The original list is : " +  str(test_list))
  
# using list comprehension + enumerate()
# to remove duplicated 
# from list 
res = [i for n, i in enumerate(test_list) if i not in test_list[:n]]
  
# printing list after removal 
print ("The list after removing duplicates : " + str(res))

→ 輸出結果:
The original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The list after removing duplicates : [1, 5, 3, 6]

方法5:利用collections.OrderedDict.fromkeys()

這是完成特殊任務中最快的方法。它先是將列表中的重復項移除并返回一個字典,最后轉換成列表。這種方法對于字符串也可以進行處理。

示例代碼:

# Python 3 code to demonstrate 
# removing duplicated from list 
# using collections.OrderedDict.fromkeys()
from collections import OrderedDict
  
# initializing list
test_list = [1, 5, 3, 6, 3, 5, 6, 1]
print ("The original list is : " +  str(test_list))
  
# using collections.OrderedDict.fromkeys()
# to remove duplicated 
# from list 
res = list(OrderedDict.fromkeys(test_list))
  
# printing list after removal 
print ("The list after removing duplicates : " + str(res))

→ 輸出結果:
The original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The list after removing duplicates : [1, 5, 3, 6]

方法6:處理嵌套列表中的重復元素

對于多維列表(列表嵌套)中的重復元素去除。這里假設列表中元素(也是列表)它們具有相同的元素(但不一定順序相同)都被當做重復元素。那么下面使用 set() + sorted() 方法來完成任務。

 示例代碼:

# Python3 code to demonstrate
# removing duplicate sublist 
# using set() + sorted()
  
# initializing list
test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
                           [1, 2, 3], [3, 4, 1]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using set() + sorted()
# removing duplicate sublist
res = list(set(tuple(sorted(sub)) for sub in test_list))
  
# print result
print("The list after duplicate removal : " + str(res)) 

→ 輸出結果:
The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]

也可以利用 set() + map() + sorted()

 示例代碼:

# Python3 code to demonstrate
# removing duplicate sublist 
# using set() + map() + sorted()
  
# initializing list
test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
                           [1, 2, 3], [3, 4, 1]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using set() + map() + sorted()
# removing duplicate sublist
res = list(set(map(lambda i: tuple(sorted(i)), test_list)))
  
# print result
print("The list after duplicate removal : " + str(res))

→ 輸出結果:
The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]

到此這篇關于Python中移除List重復項的五種方法的文章就介紹到這了,更多相關Python 移除List重復項 內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python 的topk算法實例
  • python topk()函數求最大和最小值實例
  • python list 查詢是否存在并且并返回下標的操作
  • 解決python列表list中的截取問題
  • python 如何在list中找Topk的數值和索引

標簽:常德 黑龍江 阿里 潛江 呂梁 株洲 通遼 銅川

巨人網絡通訊聲明:本文標題《Python中移除List重復項的五種方法》,本文關鍵詞  Python,中移,除,List,重復,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python中移除List重復項的五種方法》相關的同類信息!
  • 本頁收集關于Python中移除List重復項的五種方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 同仁县| 临桂县| 荣成市| 丁青县| 莱阳市| 格尔木市| 木里| 镇江市| 临洮县| 南乐县| 乌拉特中旗| 调兵山市| 辽宁省| 额济纳旗| 囊谦县| 古丈县| 泰兴市| 舒兰市| 宾阳县| 雅江县| 五大连池市| 和林格尔县| 平潭县| 广饶县| 桑植县| 南雄市| 阜新市| 米泉市| 通海县| 合肥市| 亳州市| 朝阳市| 雅安市| 常州市| 阿勒泰市| 武山县| 建德市| 同心县| 农安县| 喀喇沁旗| 黎川县|