当前分类:300例题>>正文

Python示例:从列表中删除重复项

来源:互联网   更新时间:2023年6月22日  

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://6yhj.com/leku-p-4399.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: seo