日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python程序:從列表中刪除重復(fù)項(xiàng)

創(chuàng)新互聯(lián)python教程:

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)平輿免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

編寫一個(gè) Python 程序,從給定的列表中刪除所有重復(fù)項(xiàng)。Python 集合不允許重復(fù),所以我們可以將列表轉(zhuǎn)換為集合,然后將其轉(zhuǎn)換回列表將刪除列表重復(fù)。

# 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 使用設(shè)置輸出 移除列表中的重復(fù)項(xiàng)

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]

從列表中刪除重復(fù)項(xiàng)目的 Python 程序

這個(gè) Python 程序允許輸入列表大小和項(xiàng)目。for 循環(huán)將迭代雙工項(xiàng)。帶有 not in 運(yùn)算符的 if 語(yǔ)句檢查該值是否不在優(yōu)衣庫(kù)列表中。如果為真,則將該值追加到優(yōu)衣庫(kù)列表中。

# 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)

在這個(gè)例子中,我們使用 Python 列表理解從列表中移除重復(fù)的項(xiàng)目。這段代碼與上面的例子相同,但是我們使用了列表理解的概念。

# 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]

在本例中,我們從集合中導(dǎo)入了 OrderedDict,并使用 fromkeys 函數(shù)刪除重復(fù)項(xiàng)。別忘了把結(jié)果轉(zhuǎn)換成列表。

# 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))

使用排序從集合輸出中刪除列表中的重復(fù)項(xiàng)

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模塊都有去除重復(fù)的獨(dú)特功能,所以我們使用了相同的功能,并將結(jié)果轉(zhuǎn)換為列表。為了轉(zhuǎn)換結(jié)果,我們使用了 tolist()函數(shù)。

# 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 唯一功能輸出 刪除列表中的重復(fù)項(xiàng)

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]

使用枚舉從列表中刪除重復(fù)項(xiàng)的 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)

使用枚舉輸出刪除列表中的重復(fù)項(xiàng)

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]

本文名稱:Python程序:從列表中刪除重復(fù)項(xiàng)
標(biāo)題來(lái)源:http://www.dlmjj.cn/article/coocood.html