Python 程序:從列表中刪除重複項

編寫一個 Python 程序,從給定的列表中刪除所有重複項。Python 集合不允許重複,所以我們可以將列錶轉換為集合,然後將其轉換回列表將刪除列表重複。

# Remove Duplicates from List

dupList = [1, 2, 3, 2, 4, 8, 9, 1, 7, 6, 4, 5]
print("List Items = ", dupList)

uniqSet = set(dupList)
uniqList = list(uniqSet)

print("List Items after removing Duplicates = ", uniqList)

Python 使用設置輸出 移除列表中的重複項

List Items =  [1, 2, 3, 2, 4, 8, 9, 1, 7, 6, 4, 5]
List Items after removing Duplicates =  [1, 2, 3, 4, 5, 6, 7, 8, 9]

從列表中刪除重複項目的 Python 程序

這個 Python 程序允許輸入列表大小和項目。for 循環將迭代雙工項。帶有 not in 運算符的 if 語句檢查該值是否不在優衣庫列表中。如果為真,則將該值追加到優衣庫列表中。

# Remove Duplicates from List

dupList = []

listNumber = int(input("Enter the Total List Items = "))
for i in range(1, listNumber + 1):
    listValue = int(input("Enter the %d List Item = " %i))
    dupList.append(listValue)

print("List Items = ", dupList)

uniqList = []

for val in dupList:
    if val not in uniqList:
        uniqList.append(val)

print("List Items after removing Duplicates = ", uniqList)

在這個例子中,我們使用 Python 列表理解從列表中移除重複的項目。這段代碼與上面的例子相同,但是我們使用了列表理解的概念。

# Remove Duplicates from List

dupList = [1, 2, 5, 8, 1, 9, 11, 5, 22, 6, 2, 8, 14]

print("List Items = ", dupList)

uniqList = []
[uniqList.append(i) for i in dupList if i not in uniqList]

print("List Items after removing Duplicates = ", uniqList)
List Items =  [1, 2, 5, 8, 1, 9, 11, 5, 22, 6, 2, 8, 14]
List Items after removing Duplicates =  [1, 2, 5, 8, 9, 11, 22, 6, 14]

在本例中,我們從集合中導入了 OrderedDict,並使用 fromkeys 函數刪除重複項。別忘了把結果轉換成列表。

# Remove Duplicates from List

from collections import OrderedDict

dupList = [8, 1, 9, 2, 8, 4, 9, 11, 5, 22, 6, 4, 8]

print("List Items = ", dupList)

uniqList = OrderedDict.fromkeys(dupList)

print("List Items after removing Duplicates = ", list(uniqList))

使用排序從集合輸出中刪除列表中的重複項

List Items =  [8, 1, 9, 2, 8, 4, 9, 11, 5, 22, 6, 4, 8]
List Items after removing Duplicates =  [8, 1, 9, 2, 4, 11, 5, 22, 6]

numpy 和pands模塊都有去除重複的獨特功能,所以我們使用了相同的功能,並將結果轉換為列表。為了轉換結果,我們使用了 tolist()函數。

# Remove Duplicates from List

import numpy as np
import pandas as pd

dupList = [1, 2, 2, 4, 1, 5, 6, 8, 6, 8, 9, 7, 4]
print("List Items = ", dupList)

uniqList = np.unique(dupList).tolist()
print("List Items after removing Duplicates = ", uniqList)

uniqList2 = pd.unique(dupList).tolist()
print("List Items after removing Duplicates = ", uniqList2)

使用 numpy 唯一功能輸出 刪除列表中的重複項

List Items =  [1, 2, 2, 4, 1, 5, 6, 8, 6, 8, 9, 7, 4]
List Items after removing Duplicates =  [1, 2, 4, 5, 6, 7, 8, 9]
List Items after removing Duplicates =  [1, 2, 4, 5, 6, 8, 9, 7]

使用枚舉從列表中刪除重複項的 Python 程序。

# Remove Duplicates from List

from collections import OrderedDict

dupList = [1, 2, 3, 2, 4, 1, 5, 6, 5, 8, 7, 9, 8]

print("List Items = ", dupList)

uniqList = [val for x, val in enumerate(dupList) if val not in dupList[:x]]

print("List Items after removing Duplicates = ", uniqList)

使用枚舉輸出刪除列表中的重複項

List Items =  [1, 2, 3, 2, 4, 1, 5, 6, 5, 8, 7, 9, 8]
List Items after removing Duplicates =  [1, 2, 3, 4, 5, 6, 8, 7, 9]

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/254192.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-14 17:40
下一篇 2024-12-14 17:41

相關推薦

  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python編程二級證書考試相關現已可以上網購買

    計算機二級Python考試是一項重要的國家級認證考試,也是Python編程的入門考試。與其他考試一樣,Python編程二級證書的考生需要進入正式考試,而為了備考,這篇文章將詳細介紹…

    編程 2025-04-29
  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python讀取CSV數據畫散點圖

    本文將從以下方面詳細闡述Python讀取CSV文件並畫出散點圖的方法: 一、CSV文件介紹 CSV(Comma-Separated Values)即逗號分隔值,是一種存儲表格數據的…

    編程 2025-04-29

發表回復

登錄後才能評論