Python程序:从列表中删除重复项

创新互联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]

当前文章:Python程序:从列表中删除重复项
网站URL:http://www.shufengxianlan.com/qtweb/news20/256170.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联